Thursday, August 21, 2014

Ribbon Button with Popup Operations Model Dialogue

            Ribbon Button with Popup Operations Model Dialogue

Elements File
-----------------------------------------------------------------------------------------------------
  
  <CustomAction
    Id="DeleteRestoreCustomRibbonButton"
    RegistrationId="101"
    RegistrationType="List"
    Location="CommandUI.Ribbon"
    Sequence="5"
    Title="Delete/Restore"
     Rights="ManageLists" >

    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
          <Button
              Id="Ribbon.DeleteRestoreButton"
              Alt="Delete/Restore"
              Sequence="5"
              Command="Rku_Test_Button"
              Image32by32="/_layouts/images/WinEarth32x32.png"
              Image16by16="/_layouts/images/WinEarth16x16.png"
              LabelText="Delete/Restore"
              TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>

      <CommandUIHandlers>
        <CommandUIHandler
          Command="Rku_Test_Button"
          CommandAction="javascript:
         
              function demoCallback(dialogResult, returnValue)
              {
                SP.UI.Notify.addNotification('Operation Successful!');
               
                SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
              }
             
              var ctx = SP.ClientContext.get_current();
              var items = SP.ListOperation.Selection.getSelectedItems(ctx);
              var myItems = '';
              var k;
             
              for (k in items)
              {
                myItems += '|' + items[k].id;
              }
             
              var options = {             
                url: '/_layouts/RKU/DeleteRestore.aspx?items=' + myItems + '&amp;source=' + SP.ListOperation.Selection.getSelectedList(),
                tite: 'Move Documents',
                allowMaximize: false,
                showClose: false,
                width: 680,
                height: 250,
                dialogReturnValueCallback: demoCallback };
             
              SP.UI.ModalDialog.showModalDialog(options);"
 
          //Single Item Section Enables the Button:

        EnabledScript="javascript:
        function enableBtn()
        {
        var context = SP.ClientContext.get_current();
        return SP.ListOperation.Selection.getSelectedItems(context).length == 1;
        }
        enableBtn();
        "
        />
      </CommandUIHandlers>

    </CommandUIExtension>
  </CustomAction>
 
  
  
---------------------------------------------------------------------------------------------------------------
Page:
====================
<table>
    <tr>
        <td>
            <asp:Label runat="server" ID="lblResoreSelect" Visible="false"><b>Restore Selected Document:</b></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label runat="server" ID="lblDeleteSelect" Visible="false"><b>Delete Selected Document:</b></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblSelectedItems" runat="server" Text=""></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label for="Text" runat="server" ID="lblReason"><b>Please Select a Deletion Reason from the following:</b></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:RadioButtonList ID="radiBtnSelect" runat="server">
                <asp:ListItem>Duplicate of an existing document in the source</asp:ListItem>
                <asp:ListItem>Early draft or preliminary versions which have been transformed into a final document</asp:ListItem>
                <asp:ListItem>Notes and calculations of a minor or transitory nature used in preparation of more substantial document</asp:ListItem>
                <asp:ListItem>Non business document uploaded in error</asp:ListItem>
            </asp:RadioButtonList>
        </td>
    </tr>
    <tr>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                ControlToValidate="radiBtnSelect" ErrorMessage="Please select a Deletion Reason"></asp:RequiredFieldValidator></td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="btnOK" runat="server" Text="OK" OnClick="btnOK_Click" />
            &nbsp;<input id="btnCancel" type="button" value="Cancel" onclick="ClosePopup();" />

        </td>
    </tr>
</table>
<script type="text/javascript">

    function ClosePopup() {
        window.parent.location = window.parent.location; window.close();
    }

</script>



================================================================================


Page Load:
=========
  protected string OGDeteteRestoreLibrary = "GOADocs";
        protected System.Collections.Generic.List<SPListItem> ListItems;

        protected void Page_Load(object sender, EventArgs e)
        {
            // Close SharePoint Popup Window
            if (Page.IsPostBack)
            {
                string script = "<script language='javascript'>window.parent.location = window.parent.location; window.close();</script>";
                if (!this.Page.ClientScript.IsClientScriptBlockRegistered("rKey"))
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "rKey", script);
            }

            if (Request.QueryString["items"] != null && Request.QueryString["source"] != null)
            {
                string source = Request.QueryString["source"];
                string[] items = Request.QueryString["items"].ToString().Split('|');

                source = source.Substring(1, source.Length - 2).ToLower();

                Guid sourceID = new Guid(source);

                SPDocumentLibrary sourceDocLib = (SPDocumentLibrary)SPContext.Current.Web.Lists[sourceID];

                ListItems = new System.Collections.Generic.List<SPListItem>();
                //
                for (int i = 1; i < items.Length; i++)
                {
                    SPListItem currentItem = sourceDocLib.GetItemById(int.Parse(items[i]));
                    SPListItem itemStatus = Web.Lists.TryGetList(OGDeteteRestoreLibrary).GetItemById(currentItem.ID);
                    if (itemStatus["Deleted"] != null && itemStatus["Deleted"].Equals("Deleted"))
                    {
                        // Restore Operation
                        lblResoreSelect.Visible = true;
                        radiBtnSelect.Visible = false;
                        lblReason.Visible = false;
                        SPListItem currentListItem = sourceDocLib.GetItemById(int.Parse(items[i]));
                        ListItems.Add(currentListItem);
                        lblSelectedItems.Text += currentListItem.Name + "<br>";
                    }
                    else
                    {
                        lblDeleteSelect.Visible = true;
                        SPListItem currentListItem = sourceDocLib.GetItemById(int.Parse(items[i]));
                        ListItems.Add(currentListItem);
                        lblSelectedItems.Text += currentListItem.Name + "<br>";
                    }
                    break;
                }

            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (Request.QueryString["items"] != null && Request.QueryString["source"] != null)
                {
                    string source = Request.QueryString["source"];
                    string[] items = Request.QueryString["items"].ToString().Split('|');

                    source = source.Substring(1, source.Length - 2).ToLower();

                    Guid sourceID = new Guid(source);

                    SPDocumentLibrary sourceDocLib = (SPDocumentLibrary)SPContext.Current.Web.Lists[sourceID];

                    ListItems = new System.Collections.Generic.List<SPListItem>();

                    for (int i = 1; i < items.Length; i++)
                    { // Delete Successfull
                        string siteURL = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
                        SPListItem currentListItem = sourceDocLib.GetItemById(int.Parse(items[i]));
                        SPListItem updateItem = Web.Lists.TryGetList(OGDeteteRestoreLibrary).GetItemById(currentListItem.ID);
                        if (updateItem["Deleted"] != null && updateItem["Deleted"].Equals("Deleted"))
                        { // Delete documets
                            btnOK.CausesValidation = false;
                            updateItem["Deleted"] = "";
                            updateItem.Update();

                            string script = "<script language='javascript'>window.close();</script>";
                            Page.ClientScript.RegisterClientScriptBlock(GetType(), "Register", script);

                        }
                        else
                        {  // Restore Successfull
                            updateItem["Deleted"] = "Deleted";

                            if (updateItem["Reasons_x0020_for_x0020_Delete"] == null)
                            {
                                updateItem["Reasons_x0020_for_x0020_Delete"] = radiBtnSelect.SelectedValue.ToString();
                            }
                            else
                            {
                                updateItem["Reasons_x0020_for_x0020_Delete"] = updateItem["Reasons_x0020_for_x0020_Delete"] + ". @:" + radiBtnSelect.SelectedValue.ToString();
                            }
                            btnOK.CausesValidation = false;
                            updateItem["Deleted"] = "Deleted";
                            updateItem["Deleted_x0020_Date"] = DateTime.Now;
                            updateItem["Deleted_x0020_By"] = Web.CurrentUser;
                            updateItem.Update();
                        }
                        break;
                    }
                }
            });
        }

No comments:

Post a Comment