Showing posts with label JSOM. Show all posts
Showing posts with label JSOM. Show all posts

Tuesday, August 20, 2013

Remove an user from item level permissions using JSON.

To delete a user from item level permissions using JSOM , use the following code.

var oUser= clientContext.get_web().ensureUser('your name@yourcompany.com');
oListItem.get_roleAssignments().getByPrincipal(oUser).deleteObject(); // oListItem- List item reference.

clientContext.load(oListItem);

Happy coding !!!

Tuesday, July 30, 2013

JSOM Intellisense in VisualStudio

It should be very easy for you to reference the following piece of comment, will show you intellisense in Visual Studio 2010 and above to work with SharePoint  Client applications using JSOM. If you are developing SharePoint Client applications, make sure you have installed SharePoint client SDK as described in my earlier posts or working locally on a machine in which SharePoint is installed.

Add a new *.JS file to the project, make a reference as given in the picture, and make sure the path to the SharePoint JavaScript file is right.



 

Assigning Item Level permission using JSOM in Sharepoint 2013

This post contains the code to assign Item level permission using JSOM. 




function assignItemLevelPermission()
{
              
                var clientContext = new SP.ClientContext.get_current();
                var collRoleDefinitionBinding;
                var objListItem="your list item"
                collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
                    collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.reader));
                 // Make sure you pass on the user identity as you@youroffice.com
                 var oUser= clientContext.get_web().ensureUser(“you@youroffice.com”);                
                objListItem.breakRoleInheritance(false);
                objListItem.get_roleAssignments().add(oUser, collRoleDefinitionBinding);
                objListItem.update();
                clientContext.load(oUser);
                clientContext.load(objListItem);
                clientContext.executeQueryAsync
                                                (
                                                                function onQuerySucceeded() {
                                                                   alert(‘success’);
                                                                },
                                                                function onQueryFailed() {
                                                                    alert(‘failed’);
                                                                }
                                                );

}

Monday, July 29, 2013

Create a DocumentSet(Predefined) using JSOM in SharePoint 2013 (on-premise/on-line).



This post explains how we can create a documentset(predefined) in a folder using JSOM. As in my previous post  you can apply a technical solution which can be remote/local. This solution can be applied for on-premise and as well as SharePoint on-line.

There might be business cases like you have to create a set of documents when you create a project. Assuming this business scenario, I have created a SharePoint List called ProjectMgmt and a document library called ProjectLibrary. I have configured the documentset content type with some predefined documents.

I have created a new form for the ProjectMgmt list and in that I have added the following code. I have commented out the default SharePoint Save button and included an HTML input button and named it as “Save”.

<input type="button" value="Save"  id="CreateDocumentSet" name="bCreateDocumentSet" onclick="javacript:CreateFolder();" />

Under the content place holder - placeholder main I have referenced the following scripts
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/SP.DocumentManagement.js"></script>
<script type="text/javascript" src="/layouts/15/app.js"></script>
<script type="text/javascript"src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

The code below, is to create a folder and then the document set inside the folder. I have left out the JSOM code for creating an item in a SharePoint list which is available here.  I have to create a folder with the project name and prefix the project name for the document set. You could see that I have used the documentset content type id "0x0120D520" to create the document set.


<script type="text/javascript">                
                var clientContext;
                var ProjectName;
                var ProjectID;   
                var folderUrl;
                 
                /*
                Function name :CreateFolder
                Parameters      : context , arguments
    Description      : This function is responsible for the creation of the folder. 
                           On success calls the onQuerySucceededCreateDocumentSet.          
                Returns           : None
                */
                function CreateFolder(sender, args)
                {
                      clientContext = new SP.ClientContext.get_current();
                      oList = clientContext.get_web().get_lists().getByTitle('ProjectLibrary');                                  
                      
                       //Acquire the values entered by the user
                       ProjectID =$("input[title='ProjectID']").val();
                       ProjectName=$("input[title='ProjectName']").val();      

                       var web = clientContext.get_web();
                       var newDocSetName =$("input[title='ProjectName']").val();
                       var list = web.get_lists().getByTitle('ProjectLibrary');
                       clientContext.load(list);
                       itemCreateInfo = new SP.ListItemCreationInformation();
                               
                       //Create the folder
                       itemCreateInfo.set_underlyingObjectType                                                     (SP.FileSystemObjectType.folder);
                       itemCreateInfo.set_leafName(newDocSetName);
                       this.oListItem = list.addItem(itemCreateInfo);
                       this.oListItem.update();                   
                       clientContext.load(this.oListItem);         
                       clientContext.executeQueryAsync(
                                    Function.createDelegate(this,                                                  this.onQuerySucceededCreateDocumentSet),
                                    Function.createDelegate(this,                                          this.onQueryFailedFolderCreation)
    );   
                }

                function onQueryFailedFolderCreation(sender, args)
                {             
                                alert('Folder Creation Failed due to the error: ' + args.get_message());
                }

                /*
                Function name                   : onQuerySucceededCreateDocumentSet
                Parameters                        : context
                Description                        : This function is responsible for the creation of the Document Set
                Returns                              : None
                */
                function onQuerySucceededCreateDocumentSet(param)
                {              var docSetContentTypeID = "0x0120D520";
                                clientContext = new SP.ClientContext.get_current();
                                var web = clientContext.get_web();      
                                

                                folderUrl = “YOUR SITE URL” + “/YOUR DOCUMENT LIBRARY/"
                                                   ProjectName;

                                var folder= web.getFolderByServerRelativeUrl(folderUrl);
                                clientContext.load(folder);
                                var newDocSetName =ProjectName + '-Documentset';
                                var docsetContentType = web.get_contentTypes().getById(docSetContentTypeID);     
                                clientContext.load(docsetContentType);             
               
                                clientContext.executeQueryAsync
                                (             
                                                function ()
                                                {
                                                                //Create the document set.
var isCreated = SP.DocumentSet.DocumentSet.create(clientContext, folder , newDocSetName,                                                               docsetContentType.get_id());  
                                                                clientContext.executeQueryAsync(
                                                               
                                                 function onQuerySucceededDocumentSetCreated()
                                                 {
                                                        $("#overlay").css("display","none");
                                                        alert('DocumentSet has been Created');
                                                 },
                                                               
                                                  function onQueryFailedDocumentSetCreation(sender, args)
                                                  {
                                                          $("#overlay").css("display","none");
                                                          alert('Document Set Creation Failed due to the error: ' +                                                                                           args.get_message());
                                                   }

                               );                                                                                                            
                                                },
                                               

                                                function ()
                                                {
                                                                $("#overlay").css("display","none");
                                                                alert('Folder loading failed');
                                                }

                                );            
                }
                </script>


This code works in SharePoint 2013 on-premises and also on SharePoint 2013 on-line. Happy coding!!!