Tuesday, April 9, 2013

Update List Permissions in SharePoint Hosted Apps SP2013

Update List Permissions in SharePoint Hosted Apps 

In this Post we'll update the permissions of the list and give Administrator Access to SharePoint Users. Here is how,

In Previous Post I've mentioned on how to add People Picker Control in SharePoint Hosted App.
Check it http://hasan-nasir.blogspot.com/2013/04/how-to-add-client-people-picker-in.html

1:  <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">  
2:    <SharePoint:ClientPeoplePicker AllowEmailAddresses="true" Required="true" ValidationEnabled="true" ID="peoplePicker"   
3:      runat="server" VisibleSuggestions="3" Rows="1" PrincipalAccountType="User,DL,SecGroup,SPGroup" AllowMultipleEntities="true" CssClass="ms-long ms-spellcheck-true" Height="85px" />  
4:    <input type="button" onclick="UpdatePermisions();" title="UpdatePermisions" />  
5:  </asp:Content>  

Now we'll see how to Break Permissions Inheritance of the list and how to add users in the list and give them administrator access.


1:  'use strict';  
2:  var clientContext = SP.ClientContext.get_current();  
3:  var oWebsite = clientContext.get_web();  
4:  // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
5:  $(document).ready(function () {  
6:  });  
7:  function onUpdatePermissionSuccess() {  
8:    alert("Permission of the list Updated");  
9:  }  
10:  function onUpdatePermissionFail(sender, args) {  
11:    alert('Permission Update Failed. Error: ' + args.get_message() + '\n' + args.get_stackTrace());  
12:  }  
13:  function UpdatePermisions() {  
14:    var oList = oWebsite.get_lists().getByTitle('ListName');  
15:    oList.breakRoleInheritance(false, true);  
16:    var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.ctl00_PlaceHolderMain_peoplePicker_TopSpan;  
17:    var users = peoplePicker.GetAllUserInfo();  
18:    var userInfo = '';  
19:    for (var i = 0; i < users.length; i++) {  
20:      var user = users[i];  
21:      var userobj = oWebsite.ensureUser(user["Key"].split('|')[2]);  
22:      var role = SP.RoleDefinitionBindingCollection.newObject(clientContext);  
23:      role.add(oWebsite.get_roleDefinitions().getByType(SP.RoleType.administrator));  
24:      oList.get_roleAssignments().add(userobj, role)  
25:    }  
26:    clientContext.executeQueryAsync(onUpdatePermissionSuccess, onUpdatePermissionFail);  
27:  }  

1 comment: