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/

Create Promoted Links/My Report Library/Asset Library list in SharePoint 2013 using Client Object Model C#

I have already written a blog specifying how to create a List for SharePoint using Client Object Model. You can check that here.Though that code worked for most of the Lists, the List, Promoted Links, Report Library, and Asset Library, could not be created. They were constantly throwing the error, Invalid list template! As far as I was concerned, ListTemplates that were passed to the ListCreationInformation were correct. So why this error?? Here, I must also mention that I was trying to create these Lists on the Web template Team-Site.

The problem was I was only passing the baseTemplate to it. However, for these Lists, I needed to mention the GUID of the Feature, that contained the schema of the new List. Following is the modified code.

Namespace:

using Microsoft.SharePoint.Client;

//use one of the templateName that suits your purpose
string templateName = "Promoted Links";string templateName = "Report Library";string templateName = "Asset Library";

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

Web web = ctx.Web;

// Get the list template by name
ListTemplate listTemplate = web.ListTemplates.GetByName(templateName);

ctx.Load(listTemplate);
ctx.ExecuteQuery();

// Create a new object for ListCreationInformation class - used to specify the // properties of the new list
ListCreationInformation creationInfo = new ListCreationInformation();

// Specify the title of your List
creationInfo.Title = "My Custom List";

// Specify the list description, if any
creationInfo.Description = "Description";

// Set a value that specifies the feature identifier of the feature 
// that contains the list schema for the new list.
creationInfo.TemplateFeatureId = listTemplate.FeatureId;

// Set a value that specifies the list server template of the new list
creationInfo.TemplateType = listTemplate.ListTemplateTypeKind;

web.Lists.Add(creationInfo);
ctx.ExecuteQuery();