Latest Posts

Create Permission Level in SharePoint using JSOM

The example in this topic show how to use JSOM to Create Permission Level in SharePoint
  • You can use Content Editor or Script Editor web part on your site, put Html and JSOM (Java Script Object Model) code and that's it. You can perform any action supported by JSOM APIs

How to execute JSOM code in SharePoint?

  • Note: This is just a sample, you can go ahead with your implementation.
  • Open your Site. Create a webpart page.
  • Put a Content Editor Web part and a Script Editor Web part on it.
  • Webpart page content editor and scipt editor
  • I have added below Html in Content Editor webpart. You can replace MainFunction with your actual method name.

    <button onclick="MainFunction();" type="button">Click me​</button>

  • And below references in Script Editor Webpart. You can keep jQuery reference as per your need. ScriptFile.js is a JavaScript file where you can keep below function i.e. your actual logic.

    <script src="https://code.jquery.com/jquery-2.2.4.js" type="text/javascript"></script>

    <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>

    <script type="text/javascript" src="/_layouts/15/sp.js"></script>

    <script src="https://MyServer/sites/SiteCollection/style library/js/ScriptFile.js"></script>

  • Once you are done with setting your page, click on the button and your method should get executed.

  • function CreatePermissionLevel() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     var clientContext = new SP.ClientContext();
  •  
  •     // BasePermissions Object
  •     var oBasePermissions = new SP.BasePermissions();
  •  
  •     //// Select the permissions to include in this permission level
  •     //// Below is the list of permission levels
  •  
  •     //        emptyMask     -     Has no permissions on the Web site. Not available through the user interface.
  •     //        viewListItems -     View items in lists, documents in document libraries, and Web discussion comments.
  •     //        addListItems  -     Add items to lists, add documents to document libraries, and add Web discussion comments.
  •     //        editListItems -     Edit items in lists, edit documents in document libraries, edit Web discussion comments in documents, and customize Web Part Pages in document libraries.
  •     //        deleteListItems      -     Delete items from a list, documents from a document library, and Web discussion comments in documents.
  •     //        approveItems  -     Approve a minor version of a list item or document.
  •     //        openItems     -     View the source of documents with server-side file handlers.
  •     //        viewVersions  -     View past versions of a list item or document.
  •     //        deleteVersions       -     Delete past versions of a list item or document.
  •     //        cancelCheckout       -     Cancel or check in a document that is checked out to another user.
  •     //        managePersonalViews  -     Create, change, and delete personal views of lists.
  •     //        manageLists   -     Create and delete lists, add or remove columns in a list, and add or remove public views of a list.
  •     //        viewFormPages -     View forms, views and application pages, and enumerate lists.
  •     //        open   -     Open a Web site, list, or folder to access items inside the container.
  •     //        viewPages     -     View pages in a Web site.
  •     //        addAndCustomizePages -     Add, change, or delete HTML pages, and edit the Web site using an editor that is compatible with Microsoft SharePoint Server 2010.
  •     //        applyThemeAndBorder  -     Apply a theme or borders to the Web site.
  •     //        applyStyleSheets     -     Apply a style sheet (.css file) to the Web site.
  •     //        viewUsageData -     View reports on Web site usage.
  •     //        createSSCSite -     Create a Web site using Self-Service Site Creation.
  •     //        manageSubwebs -     Create subsites such as team sites, Meeting Workspace sites, and Document Workspace sites.
  •     //        createGroups  -     Create a group of users that can be used anywhere within the site collection.
  •     //        managePermissions    -     Create and change permission levels on the Web site and assign permissions to users and groups.
  •     //        browseDirectories    -     Enumerate files and folders on a site using Microsoft SharePoint Designer and WebDAV interfaces.
  •     //        browseUserInfo       -     View information about the users of the Web site.
  •     //        addDelPrivateWebParts      -     Add or remove personal Web Parts on a Web Part Page.
  •     //        updatePersonalWebParts     -     Update Web Parts to display personalized information.
  •     //        manageWeb     -     Grant the ability to perform all administration tasks for the Web site and manage Web site content.
  •     //        useClientIntegration -     Launch client applications on the server. Otherwise, users must work on documents locally and upload changes.
  •     //        useRemoteAPIs -     Use SOAP, WebDAV, or Microsoft SharePoint Designer interfaces to access the Web site.
  •     //        manageAlerts  -     Manage alerts for all users of the Web site.
  •     //        createAlerts  -     Create e-mail alerts.
  •     //        editMyUserInfo       -     Change user information, such as adding a picture.
  •     //        enumeratePermissions -     Enumerates permissions on the Web site, list, folder, document, or list item.
  •     //        fullMask      -     Has all permissions on the Web site. Not available through the user interface.
  •  
  •     oBasePermissions.set(SP.PermissionKind.viewListItems);
  •     oBasePermissions.set(SP.PermissionKind.addListItems);
  •  
  •     // New "RoleDefinitionCreationInformation" Object
  •     var oRoleDefinitionCreationInformation = new SP.RoleDefinitionCreationInformation();
  •  
  •     // Set Role definition name
  •     oRoleDefinitionCreationInformation.set_name('Custom Role');
  •  
  •     // Set Role definition description
  •     oRoleDefinitionCreationInformation.set_description('Custom Role Description');
  •  
  •     // set role definition permission level
  •     oRoleDefinitionCreationInformation.set_basePermissions(oBasePermissions);
  •  
  •     var oRoleDefinition = clientContext.get_web().get_roleDefinitions().add(oRoleDefinitionCreationInformation);
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •     console.log('Success');
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013

We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint