Wednesday, January 15, 2014

Manage User Custom Actions SharePoint 2013 App and Sandbox - JSOM

This Project provides a simple example Form on how we can Manage User Custom actions in SharePoint Sites without using declarative Elements from SharePoint Solutions (.WSP) and Manage using JSOM API Object from SharePoint.

This project already have a lot of years "was made in 2010" since JSOM for SharePoint was introduced with SharePoint 2010, but wanted to upgrade for SharePoint 2013 with the new tokens and ID locations and upgrade to SharePoint App.


For upgrade purpose i kept the Sandbox solution with some declarative XML "no server code" everything is done with JSOM .

For a example of this solution i would like to recommend the sample in the Office 365 Developer Patterns and Practices (PnP)


This Solution can be deployed in SharePoint on-Premise and Office 365 and site Owner Permissions are required.

This example only manage the Object Web (UserCustomAction)
PS: this is a example and i know UserCustomAction are in Lists and SiteCollections but don't have the time and patient to make "Project was made in 3 hours between kikaninchen programme".... 

App

  • ManageUserCustomActionApp
    • Open Project in Visual Studio 2012/2013
    • Edit Properties of solution "ManageUserCustomActionApp" and add URL for the App
    • Give web trust to SharePoint App
    • Access in to Settings > Site Contents > Manage User Custom Action Form
    • Redirect to App Manage User Custom Action.

Sandbox

  • ManageUserCustomActionSandbox (declarative Solution, no Server Code)
    • Open Project in Visual Studio 2012/2013
    • Edit Properties of solution "ManageUserCustomActionSandox" and add URL for the Sandbox
    • Deploy Sandbox Solution 
    • Access in to Settings > Site Settings > Site Actions > Manage site features and activate Feature

  • Access to Site Settings > Site Collection Administration > Manage Custom Actions to access to access the "Manage Custom Action Page"


Visual Studio Solution in 2 Projects


Here some code examples about this solution about manage UserCustomActions.

Call Host Site SharePoint userCustomActions object cross site scripting from SharePoint-Hosted

Example:
    var context = new SP.ClientContext(appweburl);
    var factory =new SP.ProxyWebRequestExecutorFactory(appweburl);
    context.set_webRequestExecutorFactory(factory);
    var app = new SP.AppContextSite(context, hostweburl);
    site = app.get_web();
    this.collUserCustomAction = site.get_userCustomActions();

    context.load(site,'UserCustomActions');
    context.executeQueryAsync(
        EditCustomAction, onQueryFailed

    );

Add Rigths to CustomAction using SharePoint JSOM - ECMAscript

Example:
var UserCustomActions = site.get_userCustomActions();
.....
    var permissions = new SP.BasePermissions();
    permissions.set(SP.PermissionKind.viewListItems);
    permissions.set(SP.PermissionKind.addListItems);
    permissions.set(SP.PermissionKind.manageWeb);
    newUserCustomAction.set_rights(permissions);
.....
    newUserCustomAction.update();


How to get Permission names from SP.BasePermissions


When JSON call is made to UserCustomActions Object
 using "context.load(site,'UserCustomActions');" will return a int64 value but not specify each permission "viewListItems,addListItems,manageWeb". 





To get each permission this function return the rights from UserCustomAction object and return their Title.
To support this permission a array with permissions description and ID is used to validate the "SP.BasePermissions Rights".
Example:

//Get the rigths Object 
function EditCustomAction()
{
var customActionEnumerator = collUserCustomAction.getEnumerator();

    while (customActionEnumerator.moveNext()) 
    {
        var oUserCustomAction = customActionEnumerator.get_current();
            
        if (oUserCustomAction.get_id() == id) 

        {
            //Get rigths from SP.BasePermissions Object
             var rights = oUserCustomAction.get_rights();
             GetRights(rights);
        }
    }
}

//Array with all permissions and ID
var availableRights = ["emptyMask:0","viewListItems:1","addListItems:2","editListItems:3",.....]

//SP.BasePermissions
function GetRights(rights){

 for (var i=1;i<availableRights.length;i++)
 {
  var arr = availableRights[i].split(':');
  if(rights.has(parseInt(arr[1])))
  {
   alert(availableRights[i]);
  }
 }

}


values that specifies the permission to check.

Description of the Solution

This Project provides a simple example Form on how we can Manage User Custom actions in SharePoint Sites without using declarative Elements from SharePoint Solutions (.WSP) and Manage using JSOM API Object from SharePoint.


Create New Custom Action

Select the option "New Action" and the form fields will be clear and the button "Create" will be enable.
Fields to be fill-out:

Name Title and Description:name, title and description of the custom action.
Locations: location of the custom action.

Registration Type, Registration Id: Identifier and type of object associated with the custom action.
Rights:permissions needed for the custom action.


Script Block and ScriptSrc: ECMAScript to be executed when the custom action is performed and URI of a file which contains the ECMAScript to execute on the page.

Sequence and UrL Action:  value that determines the order of the custom action that appears on the page and URL, URI, or ECMAScript (JScript, JavaScript) function associated with the action.

Command UI Extension:  XML fragment that determines user interface properties of the custom action.

Update Custom Action 

Select a Custom Action in the Left Box to be updated.
The follwoing options became enable:
  • Update
  • Delete

Make the changes and select the option "Update" to update the Custom Action.
After the option is select the Left list is Updated.

Delete Custom Action

To delete a Custom Action, select a Custom Action and select the option "Delete".


This option has a validation question before the item is deleted.
After confirmation the item will disapear from the Custom Action options.

Delete all Custom Actions 

To delete all Custom Action associated in the Object Web, select the option "Delete All Custom Actions".



Generate Declarative 

The option "Generate Declarative XML" creates a Element XML with the configuration of the Custom Action.

CustomAction Delete All UserCustom Actions

If you need to delete all User Custom Actions in one action the following code JSOM code with method .clear() .

 var context = new SP.ClientContext(appweburl); 
        var factory =new SP.ProxyWebRequestExecutorFactory(appweburl); 
        context.set_webRequestExecutorFactory(factory); 
        var app = new SP.AppContextSite(context, hostweburl); 
        site = app.get_web(); 
        var UserCustomActions = site.get_userCustomActions(); 
        UserCustomActions.clear(); 
         
           context.executeQueryAsync( 
            onQuerySuccess, onQueryFailed 
        ); 

CustomActions examples

This solution has 4 CustomActions Examples where shows the different options and properties of the Custom Actions, this example can be activated in the option "Create User Custom Actions Examples".
  • New ScriptBlock Alert
  • New Ribbon Tab in Document Library
  • Bing It!
  • New Site
  • Manage Custom Actions (This appear in the Sanbox solution)
You can use "SharePoint 2013 Client Browser" to access your SharePoint Site using the CSOM (Client Side Object) and view the Objects.  


Support Links:
SP.UserCustomAction Properties Link
Server Ribbon XML Example: Link 
Declarative Customization of the Server Ribbon: Link
User Custom Action token Link 
Default Server Ribbon Customization Locations Link
CustomAction Locations Link
Registration Type Link
Content Type Link
List Enumeration: Link
Rights Permission Link
SharePoint Client Browser for SharePoint 2010 and 2013
Office 365 Developer Patterns and Practices (PnP)
Core.ManageUserCustomActions

Hope you like this solution,
Kind regards, 
André Lage
Post a Comment