Import SharePoint Site Search Configuration using Client Object Model C#

In my previous post, https://realmpksharepoint.wordpress.com/2014/03/03/export-sharepoint-site-search-configuration-using-client-object-model-c/, I had demonstrated how to export the Search Configuration of a SharePoint site.  Here, I will show how to import an exported search configuration.

To import the Search Configuration, we have to use the following namespace

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search.Administration; 
using Microsoft.SharePoint.Client.Search.Portability; 

In the following code, the string variable, searchConfiguration, holds the search configuration

ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);
SearchConfigurationPortability searchConfigurationPortability =

new SearchConfigurationPortability(ctx);
SearchObjectOwner searchObjectOwner = new SearchObjectOwner(ctx, SearchObjectLevel.SPWeb);

searchConfigurationPortability.ImportSearchConfiguration(searchObjectOwner, searchConfiguration);
ctx.ExecuteQuery(); 

Export SharePoint Site Search Configuration using Client Object Model C#

To export the Search Configuration, we have to use the following namespace
using Microsoft.SharePoint.Client; 
using Microsoft.SharePoint.Client.Search.Administration; 
using Microsoft.SharePoint.Client.Search.Portability; 
string searchConfiguration = String.Empty;

ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);

SearchConfigurationPortability searchConfigurationPortability = new SearchConfigurationPortability(ctx);
SearchObjectOwner searchObjectOwner = new SearchObjectOwner(ctx, SearchObjectLevel.SPSite);
ClientResult<string> crSearchConfig = searchConfigurationPortability.ExportSearchConfiguration(searchObjectOwner);
ctx.ExecuteQuery();

if (crSearchConfig != null && !String.IsNullOrWhiteSpace(crSearchConfig.Value))
{
    //Get the search configuration as string
    searchConfiguration = crSearchConfig.Value;
}

Check out my other blog at, https://realmpksharepoint.wordpress.com/2014/03/04/import-sharepoint-site-search-configuration-using-client-object-model-c/, to know how to import an exported search configuration to a site

Get the UTC DateTime of a SharePoint Field using Client Object Model C#

Following is just a sample to get the current site’s last modified date in UTC. You can apply the same to any DateTime fields in SharePoint. Here, SharePoint itself returns the DateTime in UTC as per its regional setting.
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 
RegionalSettings regionalSettings = ctx.Web.RegionalSettings;

ClientResult<DateTime> utcTime = regionalSettings.TimeZone.LocalTimeToUTC(ctx.Web.LastItemModifiedDate);

ctx.ExecuteQuery();

utcTime.Value is your DateTime in UTC.

Apply a new Theme to a SharePoint site using Client Object Model C#

In my previous post, https://realmpksharepoint.wordpress.com/2014/02/13/get-the-current-theme-properties-of-a-sharepoint-website-using-client-object-model-c/, I had described how one can get all the available themes for a SharePoint site using CSOM. Now, here, I am going to demonstrate how to Change/Modify/Apply a new Theme to a SharePoint site.Actually, applying a theme is relatively easier. There is one method in the Web class of the Microsoft.SharePoint.Client namespace which will set the theme of the site. You just have to call this method with proper parameters.

private void ApplyTheme(string colorUrl, string fontUrl, string backImageUrl)
{
  if (!String.IsNullOrEmpty(colorUrl))
  {
    this.ctx.Web.ApplyTheme(colorUrl, fontUrl, backImageUrl, false);
    this.ctx.ExecuteQuery();
  }
}

Here, few things are noteworthy:

  • First of all I am not sure about the parameter shareGenerated [I have passed false to it]. You should test it in your application to get the desired result.
  • Out of the parameters, colorUrl, fontUrl, backImageUrl, colorUrl is mandatory. You can’t set it Empty or null. If your color is null then don’t apply the theme, it’s as good as setting the default theme.
  • The other two optional parameters, fontUrl, & backImageUrl also cannot be set as String.Empty. If you do this, then, you might encounter an error Invalid Url. Hence, if you don’t want to set any, or, both of this parameters then set them as NULL.

Get the current Theme Properties of a SharePoint Website using Client Object Model C#

Well, I guess you might have discovered as of now that, there is no direct property/field in the Web class which can give you all the information about the theme of your site. SO to get the theme info, you need to know what are the components that a SharePoint site’s theme consist of.

  • MasterPage
  • CustomMasterPage
  • Theme
  • Background-Image
  • Font
So to know the site current theme using CSOM you need to get the List, Composed Looks, for the current site. Composed Looks will have various items, each of them are a different theme that can be applied to the current site. One of the items it has is, Current. It is this ListItem, which contains the current site theme info. You need to query this item to get the current theme details of a site.
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

SP.List list = ctx.Web.Lists.GetByTitle("Composed Looks");
ctx.Load(list);
CamlQuery cQuery = new CamlQuery();
ListItemCollection ltItemCollection = list.GetItems(cQuery);

//Load only the required fields
ctx.Load(ltItemCollection, w => w.Include(
p => p.DisplayName, 
p => p.FieldValuesAsText["FontSchemeUrl"], 
p => p.FieldValuesAsText["ImageUrl"], 
p => p.FieldValuesAsText["ThemeUrl"]));

ctx.Load(ctx.Web, w => w.MasterUrl, w => w.CustomMasterUrl);

ctx.ExecuteQuery();
ListItem ltItemTemp = ltItemCollection.FirstOrDefault(p => p.DisplayName == "Current");

So, finally, you’ll have the following:-

MasterPage ctx.Web.MasterUrl
CustomMasterPage ctx.Web.CustomMasterUrl
Theme ltItemTemp.FieldValuesAsText[“ThemeUrl”]
Background-Image ltItemTemp.FieldValuesAsText[“ImageUrl”]
Font ltItemTemp.FieldValuesAsText[“FontSchemeUrl”]

To apply a new theme, check my next blog at, https://realmpksharepoint.wordpress.com/2014/02/24/apply-a-new-theme-to-a-sharepoint-site-using-client-object-model-c/