Sharepoint 2013 / O365 CSOM Code Snippets


1.  Adding Group to group collection


  using (ClientContext clientContext = new ClientContext(siteCollectionUrl))
                {
                    // SharePoint Online Credentials
                    clientContext.Credentials = credentials;
                    try
                    {
                        // Loading the site collection root web
                        Web web = clientContext.Site.RootWeb;
                        clientContext.Load(clientContext.Site.RootWeb);
                        clientContext.ExecuteQuery();

                        // Getting the SiteCollection groups
                        GroupCollection siteGroups = clientContext.Web.SiteGroups;

                        // Getting the group on basis of group name
                        Group group = siteGroups.GetByName(groupName);

                        // Getting User Creation Information
                        User user = web.EnsureUser(domainGroup);

                        // Adding the domain group to group
                        group.Users.AddUser(user);
                        clientContext.Load(user, t => t.Title);
                        clientContext.ExecuteQuery();

                        // Logging the success
                    }
                    catch (Exception exception)
                    {
                    }
}

2.  Page Check through CSOM


  using (ClientContext clientContext = new ClientContext(siteCollectionUrl))
                {
                    // SharePoint Online Credentials
                    clientContext.Credentials = credentials;
                    try
                    {

Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
clientContext.ExecuteQuery();
List pages = web.Lists.GetByTitle(libraryName);
clientContext.Load(pages);
clientContext.ExecuteQuery();
ListItemCollection existingPages = pages.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(existingPages);
clientContext.ExecuteQuery();
foreach (ListItem page in existingPages)
{
       clientContext.Load(page.File);
              clientContext.ExecuteQuery();
              if (page.File.CheckOutType != CheckOutType.None)
              {
                     // Checkin and publish file
                     page.File.CheckIn("Check In Page", CheckinType.MajorCheckIn);
                     page.File.Publish("Publish Page");
                     clientContext.ExecuteQuery();
Console.WriteLine("Page is Published : " + page.File.Title);
              }
                    catch (Exception exception)
                    {
                    }
}

3.  Reset Master Page for Subsites  


   string masterPageUrl = string.Empty;
   string customPageUrl = string.Empty;
   string alternateCssUrl = string.Empty;
   string themeUrl = string.Empty;
   string fontSchemeUrl = string.Empty;
      
   using (ClientContext clientContext = new ClientContext(siteUrl))
   {
    ////Connecting to the site url
    clientContext.Credentials = credentials;
    if(isRootWeb)
    {
    Web web = clientContext.Site.RootWeb;
    ThemeInfo rootWebThemeInfo = web.ThemeInfo;
    clientContext.Load(web, t => t.MasterUrl, t=> t.CustomMasterUrl, t=> t.AlternateCssUrl, t=> t.ThemeInfo, t => t.ServerRelativeUrl);
 clientContext.Load(rootWebThemeInfo, t => t.ThemeBackgroundImageUri);
 clientContext.ExecuteQuery();

 masterPageUrl = web.MasterUrl.ToString();
 customPageUrl = web.CustomMasterUrl.ToString();
 alternateCssUrl = web.AlternateCssUrl.ToString();
 themeUrl = web.ServerRelativeUrl + "/_catalogs/theme/15/palette001ks.spcolor";
 fontSchemeUrl = web.ServerRelativeUrl + "/_catalogs/theme/15/fontscheme001ks.spfont";
                       
 }

 if (isAllWebs)
 {
       Web oWebsite = clientContext.Web;
       clientContext.Load(oWebsite, website => website.Webs, website => website.Title, website => website.Url);
       clientContext.ExecuteQuery();

       foreach (Web childWebsite in oWebsite.Webs)
       {
              try
              {
                     childWebsite.MasterUrl = masterPageUrl;
                     childWebsite.CustomMasterUrl = customPageUrl;
                     childWebsite.AlternateCssUrl = alternateCssUrl;
                     childWebsite.Update();
                     clientContext.ExecuteQuery();
childWebsite.ApplyTheme(themeUrl, fontSchemeUrl,null,false);
                     clientContext.ExecuteQuery();
              }
              catch (Exception exception)
                     {
                     }
                }
     }
     }

4.  Remove Default Site Collection Term Set


 using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = sharePointOnlineCredentails;
       Site site = clientContext.Site;

// Getting the Taxonomy Session
       TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);

       // Getting the default site collection term store
       TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();

       // Getting the term group and getting all the termsets
       TermGroup termGroup = termStore.GetSiteCollectionGroup(site, false);
       clientContext.Load(termGroup.TermSets);
       clientContext.ExecuteQuery();

       // Iterate through all the termsets and delete the termsets
       foreach (TermSet termSet in termGroup.TermSets)
       {
              // Deleting the termset
              termSet.DeleteObject();
              clientContext.ExecuteQuery();
   
       // Deleting the term group
       termGroup.DeleteObject();
       clientContext.ExecuteQuery();
            
       // Setting the Navigation for publishing web
       clientContext.Load(clientContext.Web);
       clientContext.ExecuteQuery();

       try
       {
       WebNavigationSettings webNavigationSetting = new WebNavigationSettings(clientContext, clientContext.Web);
       PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(clientContext, clientContext.Web);
       var currentNavigation = webNavigationSetting.CurrentNavigation;
       var globalNavigation = webNavigationSetting.GlobalNavigation;
clientContext.ExecuteQuery();
currentNavigation.Source = Microsoft.SharePoint.Client.Publishing.Navigation.StandardNavigationSource.PortalProvider;
       globalNavigation.Source = Microsoft.SharePoint.Client.Publishing.Navigation.StandardNavigationSource.PortalProvider;
       webNavigationSetting.Update(taxonomySession);
       clientContext.ExecuteQuery();
       }
       catch (Exception ex)
       {
       }          

       }

You May Also Like

0 comments