Tuesday, December 20, 2016

Set User as Owner of Group using JavaScript Object Model (ECMA) Programming In SharePoint 2016 And Office 365



Steps:

  •  Add Content Editor Web part (CEWP) to the SharePoint page.
  •  Save the below script as a text file and upload it to Site Assets page.
  •  Refer the script file from the CEWP.
Script:

<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', setAsOwner);
});
var oGroupUsers;
function setAsOwner() {
 //Get client context and web object
 var clientContext = new SP.ClientContext();
 var oWeb = clientContext.get_web();
 //Get group collection and specific group
 var groupCollection = oWeb.get_siteGroups();
 oGroup = groupCollection.getByName("HR Group");
//Set as owner
 var oUser =
clientContext.get_web().ensureUser('i:0#.f|membership|User@abc.on
microsoft.com');
 oGroup.set_owner(oUser);
 oGroup.update();
 //Load the client context and execute the batch
 clientContext.load(oGroup);
 clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
}
function QuerySuccess() {
 console.log("Group Owner set.");
}
function QueryFailure(sender,args) {
 console.log('Request failed'+ args.get_message());
}

</script>


#SharePoint2016 And #Office365

No comments:

Post a Comment