Convert any time format to web time zone format presents in SharePoint site

  • Posted by: Softree Technology

Generally Last modified time of SharePoint site or SharePoint list or SharePoint list item is in UTC (Universal Time Coordinated) format and if SharePoint time zone format is not UTC format then result of last modified time such as (web.LastItemModifiedDate) seems incorrect.

So it has to convert time into correct format of sharepoint web time zone.

Steps:-

  1. Add reference “Microsoft.sharepoint.client.dll” and “Microsoft.sharepoint.client.Runtime.dll”.
  2. Add namespace usingSharePoint.Client and

using Microsoft.SharePoint.Client.Utilities;

 

using System;

using System.Collections.Generic;

using Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client.Utilities;

 

namespace ConsoleApplication10

{

class Program

{

static void Main(string[] args)

{

string webLastModified = string.Empty;

using (ClientContext ctx=new ClientContext(“SiteUrl”))

{

  //Apply Credential

ctx.Credentials = new System.Net.NetworkCredential(“UserName”, “Password”);

Web web = ctx.Web;

 ctx.Load(web);

ctx.ExecuteQuery();

//Gives time in UTC Format

DateTime timeUtc = web.LastItemModifiedDate;

//Convert to sharepoint web format

  ClientResult<string> dateTime = Utility.FormatDateTime(ctx, web, timeUtc,    DateTimeFormat.DateTime);

ctx.ExecuteQuery();

 webLastModified = dateTime.Value;

}

}

}

}

Author: Softree Technology

Leave a Reply

1 Comment