Search This Blog

Sunday, March 26, 2017

Add Users to Sharepoint Group in SharePoint 2013 using CSOM

 using(ClientContext clientContext = new ClientContext("http://Test.sharepoint.com/sites/TestSite/"))
 {
            GroupCollection Groups = context.Web.SiteGroups;
            //Get the group by its name 
            Group ownersGroup = Groups.GetByName("GroupName");
            // Create the new user info  
            UserCreationInformation userCreationInfo = new UserCreationInformation();
            userCreationInfo.Email = "Parthasarathy@sharepoint.com";
            userCreationInfo.LoginName = "sharepoint\\Parthasarathy";
            userCreationInfo.Title = "User";  
            // Add the user to the group  
            User newUser = ownersGroup.Users.Add(userCreationInfo);
            context.ExecuteQuery();
 }  

Wednesday, March 22, 2017

How to assign permission to the Sharepoint group using CSOM C#


AddPermissionToGroup(clientContext, web, "Group Name", "Read");

public static void AddPermissionToGroup(ClientContext clientContext, Web web, string groupName, string Permission)
{
  try
  {
    using (clientContext)
    {
      //Get group by its name
       var group = clientContext.Web.SiteGroups.GetByName(groupName);
      //Get role by its name
       var roletypes = clientContext.Web.RoleDefinitions.GetByName(Permission);
       clientContext.ExecuteQuery();
       web.BreakRoleInheritance(true, false);
       RoleDefinitionBindingCollection collRoleDefinitionBinding = new       RoleDefinitionBindingCollection(clientContext);
       collRoleDefinitionBinding.Add(roletypes);
       web.RoleAssignments.Add(group, collRoleDefinitionBinding);
       web.Update();
       clientContext.ExecuteQuery();
    }
  }
  catch (Exception ex)
  {
     ErrorLogger.LogException(ex);
  }
}

Saturday, March 18, 2017

How to retrieve associated list name from a FieldLookupValue Sharepoint 2013 CSOM

The following example demonstrates how to retrieve associated List for Predecessors field from Tasks list:

using (var oClientContext = new ClientContext("http://Testserver/sites/Test"))
{
     var list = oClientContext.Web.Lists.GetByTitle("Tasks");
     var field = list.Fields.GetByInternalNameOrTitle("Predecessors");
     var lookupField = oClientContext.CastTo<FieldLookup>(field);
     oClientContext.Load(lookupField);
     oClientContext.ExecuteQuery();
     var lookupListId = new Guid(lookupField.LookupList); //returns associated list id
     //Retrieve associated List
     var lookupList = oClientContext.Web.Lists.GetById(lookupListId);
     oClientContext.Load(lookupList);
     oClientContext.ExecuteQuery();
}

How to determine the value entered is a User or Group via CSOM

public static string GetUserFieldType(ClientContext clientContext,FieldUserValue value)
{
    var userInfoList = clientContext.Site.RootWeb.SiteUserInfoList;
    var userInfo = userInfoList.GetItemById(value.LookupId);
    clientContext.Load(userInfo,i => i.ContentType);
    clientContext.ExecuteQuery();
    return userInfo.ContentType.Name;
}

If  the return value is Person it is a User, if it is SharePointGroup it is a Group

How to update the JSLink value of a Web Part Programmatically via CSOM in Office365

    //The below Code is used to update the JSLink property value of a webpart  programmatically using csom

    using (ClientContext oClientContext = new ClientContext(siteUrl))
    {
      //Connect to the SharePoint Online site
       oClientContext.AuthenticationMode = ClientAuthenticationMode.Default;
       oClientContext.Credentials = credentials;
       oClientContext.Load(clientContext.Web);
       oClientContext.ExecuteQuery();

      //Get the list
       List olist = oClientContext.Web.Lists.GetByTitle(“Test List”);
       oClientContext.Load(olist);
       oClientContext.Load(olist.Forms);
       oClientContext.ExecuteQuery();

      //Get all the forms
       foreach (var spForm in olist.Forms)
       {
        //Get the New form
        if (spForm.ServerRelativeUrl.Contains(“NewForm.aspx”))
        {
           File oFile = oClientContext.Web.GetFileByServerRelativeUrl(spForm.ServerRelativeUrl);
           LimitedWebPartManager wpm = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
           oClientContext.Load(wpm.WebParts,wps => wps.Include(wp => wp.WebPart.Title));
           oClientContext.ExecuteQuery();

          //Set the properties for all web parts
          foreach (WebPartDefinition wpd in wpm.WebParts)
          {
            WebPart wp = wpd.WebPart;
            wp.Properties[“JSLink”] = “~/sitecollection/Style%20Library/CustomScript.js”;
            wpd.SaveWebPartChanges();
            oClientContext.ExecuteQuery();
          }
        }

      }

    }

         

How to retrieve Lookup Field via CSOM Javascript in Sharepoint 2013


The below code is used to get lookup values from lookup field

//Get LookupField value
var oLookupField = item.get_item('FieldName');
var oLookupValue = oLookupField.get_lookupValue();
var oLookupID = oLookupField.get_lookupId();


The below code is used to get lookup values from multi lookup field

//Get Multiple Lookup Field value
var oLookupField = item.get_item('FieldName');
for(var i = 0; i < oLookupField.length; i++) {
   var oLookUpColumn = oLookupField[i];
   var oLookupValue = oLookUpColumn.get_lookupValue();
   var oLookupID = oLookUpColumn.get_lookupId();
}

How to add a value for column in Item adding event in Remote event receivers using csom



public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
SPRemoteEventResult result = new SPRemoteEventResult();
if (properties.EventType == SPRemoteEventType.ItemAdding)
{
result.ChangedItemProperties.Add("Title", "After adding");
result.Status = SPRemoteEventServiceStatus.Continue;
}
return result;
}