Wednesday, March 15, 2023

SharePoint Edit Control Block (ECB) menu or Custom Action Menu In ListItem and Site

 SharePoint Edit Control Block (ECB) menu or Custom Action Menu In ListItem and Site:

<script
  language="javascript"
  type="text/javascript"
  src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"
></script>
<script language="javascript" type="text/javascript">
  $(document).ready(function () {
    SP.SOD.executeFunc("sp.js", "SP.ClientContext", AddCustomUserActionToECB);
    SP.SOD.executeFunc("sp.js", "SP.ClientContext", ModifyUserCustomAction);
    SP.SOD.executeFunc("sp.js", "SP.ClientContext", DeleteUserCustomAction);
    SP.SOD.executeFunc("sp.js", "SP.ClientContext", CreateUserCustomActionSite);
  });

  function AddCustomUserActionToECB() {
    var clientContext = new SP.ClientContext();
    var oWeb = clientContext.get_web();
    var oList = oWeb.get_lists().getByTitle("testlist");
    var userCustomActionColl = oList.get_userCustomActions();
    var oUserCustomAction = userCustomActionColl.add();
    oUserCustomAction.set_location("EditControlBlock");
    oUserCustomAction.set_sequence(100);
    oUserCustomAction.set_title("Click Here");
    oUserCustomAction.set_url(
      "/sites/pub/Pages/Finance.aspx?ListId={ListId}&ItemId={ItemId}&amp;ItemUrl={ItemUrl}"
    );
    oUserCustomAction.update();
    clientContext.load(userCustomActionColl);
    clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
  }
  function QuerySuccess() {
    console.log("Custom Action added to ECB menu.");
  }
  function QueryFailure() {
    console.log("Request failed - " + args.get_message());
  }

  function ModifyUserCustomAction() {
    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();
    var list = web.get_lists().getByTitle("testlist");
    var CustomActionColl = list.get_userCustomActions();
    ctx.load(list, "UserCustomActions", "Title");
    ctx.executeQueryAsync(
      function () {
        var customActionEnum = CustomActionColl.getEnumerator();
        while (customActionEnum.moveNext()) {
          var custAction = customActionEnum.get_current();
          if (custAction.get_title() == "Click Here") {
            custAction.set_title("Click Here new");
            custAction.update();
            ctx.load(custAction);
            ctx.executeQueryAsync(
              function () {
                alert(
                  "Custom action modified successfully for " + list.get_title()
                );
              },
              function (a, s) {
                alert("Error " + a.get_message());
              }
            );
          }
        }
      },
      function (a, s) {
        alert("Error " + a.get_message());
      }
    );
  }

  function DeleteUserCustomAction() {
    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();
    var list = web.get_lists().getByTitle("testlist");
    var CustomActionColl = list.get_userCustomActions();
    ctx.load(list, "UserCustomActions", "Title");
    ctx.executeQueryAsync(
      function () {
        var customActionEnum = CustomActionColl.getEnumerator();
        while (customActionEnum.moveNext()) {
          var custAction = customActionEnum.get_current();
          if (custAction.get_title() == "Click Here") {
            custAction.deleteObject();
            ctx.load(custAction);
            ctx.executeQueryAsync(
              function () {
                alert(
                  "Custom action deleted successfully for " + list.get_title()
                );
              },
              function (s, a) {
                alert("Error " + a.get_message());
              }
            );
          }
        }
      },
      function (a, s) {
        alert("Error " + a.get_message());
      }
    );

    function CreateUserCustomActionSite() {
      var ctx = new SP.ClientContext.get_current();
      var web = ctx.get_web();
      var CustomActionColl = web.get_userCustomActions();
      var custAction = CustomActionColl.add();
      custAction.set_location("Microsoft.SharePoint.StandardMenu");
      custAction.set_group("SiteActions");
      custAction.set_sequence(101);
      custAction.set_title("site custom action");
      custAction.set_description("Description for custom action.");
      custAction.update();
      ctx.load(web, "Title", "UserCustomActions");
      ctx.executeQueryAsync(
        function () {
          alert("Custom action created successfully for " + web.get_title());
        },
        function (sender, args) {
          alert("Error " + args.get_message());
        }
      );
    }
  }
</script>

<input type="button" value="Create User CustomAction for list" id="create"
    onclick="CreateUserCustomActionList()" /></br ></br >
<input type="button" value="Delete User CustomAction for list" id="Delete"
    onclick="DeleteUserCustomAction()" /></br ></br >
<input type="button" value="Modify User CustomAction for list" id="Modify"
    onclick="ModifyUserCustomAction()" /></br ></br >
<input type="button" value="Create User CustomAction for site" id="Create"
    onclick="CreateUserCustomActionSite()" /></br ></br >
=========================================================
<html>
  <script language="javascript" type="text/javascript">  
    function ClearListItesm(itemID) {
      var ctx = new SP.ClientContext.get_current();
      var web = ctx.get_web();
      var oList = web.get_lists().getByTitle("testlist");
      this.oListItem = oList.getItemById(itemID);
      oListItem.set_item("address", "");
      oListItem.set_item("phone", "");
      oListItem.update();
      ctx.executeQueryAsync(QuerySuccess, QueryFailure);
    }
    function QuerySuccess() {  
        console.log("QuerySuccess");
    }
    function QueryFailure() {        
        console.log("QueryFailure" + args.get_message());
    }
    function getSelectedItemIDs() {
        var items = SP.ListOperation.Selection.getSelectedItems();
        items.forEach(item => {
            console.log(item.id);
            ClearListItesm(item.id);
        });
        location.reload();
    }
  </script>
  <input type="button" value="delete" id="update" onclick="getSelectedItemIDs()"/></br>
</html>
=========================================================

No comments:

Post a Comment

Featured Post

Sent Email in C#

Sent Email in C# : //Option 1: using S ystem; using System.Net.Mail; using System.Security; using System.Text; using System.Threading.T...

Popular posts