SharePoint 2010/2013 Date Comparison in different Time Zones

Working with DateTime in SharePoint is always tricky since it stores values internally in UTC and when we tried to access it in CSOM or SOM in different Time zone we have to be very careful while filtering on Created, Modified Date columns.

Time Zone plays very crucial roles in any Date Time difference calculation. When we do difference in two date times, it must be in same Time Zone offset otherwise the result will never be correct. Here, TimeZoneInfo class plays main role. To get specific time zone details we have to use TimeZoneInfo.FindSystemTimeZoneById method which will take input of Zone Id. All available Time Zone Id’s can be found at MSDN https://msdn.microsoft.com/en-us/library/gg154758.aspx.

To explain this problem lets take a example that we need to find all SharePoint List items which was created in last 5 minutes.

First we will retrieve all Items from a List which is created Today. We will use CAML Query to get these items.
[csharp]

// Store result in temporary Table
DataTable dataResult = new DataTable();
dataResult.Columns.Add(“ID”);
dataResult.Columns.Add(“Title”);

//Get List Data
SPWeb sbWeb = SPContext.Current.Site.RootWeb;
SPList list = sbWeb.GetList(“MyCustomList”);
SPQuery listQuery = new SPQuery();

// Get items for todays date
listQuery.Query = “<Where><Eq><FieldRef Name=’Created’ /><Value Type=’DateTime’><Today /></Value></Eq></Where>”;
SPListItemCollection items = list.GetItems(listQuery);

[/csharp]

Iterate through each list item and perform Date time conversion.

[csharp]

foreach (SPListItem item in items)
{
if (item[“Title”] != null && item[“ID”] != null)
{

// Get UTC Now Date Time
DateTime utcNowDate = new DateTime(DateTime.UtcNow.Ticks, DateTimeKind.Utc);

// Find the Server Time Zone Info which is configured under Central Administration -> Web Application -> General Settings
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(“Pacific Standard Time”);

// Convert UTC Now Date Time to server’s Time Zone i.e. into Pacific Standard Time in our example.
DateTime convertedUtcNowDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcNowDate , timeZoneInfo);

// Find the Date time difference into TimeSpan
TimeSpan diffResult = convertedUtcNowDateTime – Convert.ToDateTime(item[“Created”]);

// Create 5 Minutes Time Span object
TimeSpan fiveMinutesSpan = new TimeSpan(0, 5, 0);

// Compare method will return -1 if first time interval is shorter than second and return 0 if both time interval are equal. Read more about this at https://msdn.microsoft.com/en-us/library/system.timespan.compare(v=vs.110).aspx
int result = TimeSpan.Compare(diffResult, fiveMinutesSpan);

// If result is 0 Or -1 it means items is created in last five minutes.
if (a < 1) { DataRow row = dataResult.NewRow(); row["Title"] = item[SPBuiltInFieldId.EncodedAbsUrl].ToString(); row["ID"] = item[ColumnName.ReportUrl].ToString(); data.Rows.Add(dr); } } } [/csharp] Resulting DataTable will have the filtered result. Sharing is Caring ­čÖé

Associate Custom Content Type with SharePoint 2010/2013 List Or Library using PowerShell

To automate deployment in SharePoint 2010/2013 on-premise solution, we have to use PowerShell. One of the deployment activity is creating SharePoint Custom Lists/Libraries and associating them with custom created content types. In this article we will be focusing only how to add/remove content types from the List/Library.

To add content type follow below steps –

  • Get Reference to Site collection and root web to get the content types.

[shell]

$siteRef = Get-SPSite http://domain/site
$rootWebRef = $siteRef.RootWeb

[/shell]

  • Get the target web reference to which content type will be added for List/Library.

[shell]
$web = Get-SPWeb http://domain/site/web
[/shell]

  • Get the reference to target list / library

[shell]
$listRef = $web.Lists[“List Title”]
[/shell]

  • Enable the management of content type for this list/library.

[shell]

$listRef.ContentTypesEnabled  = $true
$listRef.Update()

[/shell]

  • Get the deployed Content Type┬áreference from root web which will be added to the target list.

[shell]
$targetContentType= $rootWebRef.ContentTypes[“CustomContentType”]
[/shell]

  • Check if Content is already exists from ContentTypes collection of a List. If not found then add the content type to the list.

[shell]
$contentTypeExist = $listRef.ContentTypes[$contentType.Name]
if($contentTypeExist -ne $null)
{
$listRef.ContentTypes.Add($targetContentType)
$listRef.Update()
}
[/shell]

To delete content type follow below steps –

To delete content type association from list, we have to get the reference of associated content type and pass the Id of content type to Delete method on ContentTypes collection.

[shell]

$contentTypeExist = $listRef.ContentTypes[$contentType.Name]

$listRef.ContentTypes.Delete($contentTypeExist.Id)

[/shell]

This is how the complete code will look like –

[shell]

$siteRef = Get-SPSite http://domain/site
$rootWebRef = $siteRef.RootWeb
$web = Get-SPWeb http://domain/site/web

$listRef = $web.Lists[“List Title”]
if ($listRef -ne $null)
{
$listRef.ContentTypesEnabled  = $true
$listRef.Update()
$targetContentType = $rootWebRef.ContentTypes[$contentType.Name]
if($targetContentType -ne $null)
{
$contentTypeExist = $listRef.ContentTypes[$contentType.Name]
if($contentTypeExist -ne $null)
{
Write-Host “Adding Content Type – ” $targetContentType.Name -ForegroundColor Green
$listRef.ContentTypes.Add($targetContentType)
}
else
{
Write-Host ”┬á Content Type already exists – ” $targetContentType.Name -ForegroundColor Green
}
}
$listRef.Update()
}

[/shell]

Happy Coding ­čÖé