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 ­čÖé

jQuery based Resource Localization

Create client side (JavaScript) based Resource Localization in any web application. Extremely light-weight library, just 3KB (Minified) and 8KB(Debug) version.

Implementing client side localization into any web application is always challenge. Most of web technologies provide localization using server-side technologies. For e.g. .Net application uses Resources Files(.resx) to implement localization into globalization application.

Nowadays world is extensively moving towards Asynchronous development models like AJAX / SPA. Several frameworks, libraries are already available on the web. To access these server side resources requires custom REST services / handlers to be additionally implemented into the host application.

This plugin makes life easier where you can manage all your localized resources into plain Text or JavaScript file. Based on current culture provided to this plugin, it loads associated resource entries for use. To enhance performance it Cache data for subsequent Initialization calls. So now no more server side development required for managing localization. It can be used in any web applications like SharePoint On-Premise / Online, Php, ASP.Net MVC, JAVA and many more as long as these technologies supports JavaScript.

Download Pugin from Github Github.

Install via Nuget Package.

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 ­čÖé

Understanding Features and Feature Scope in SharePoint 2010

What is SharePoint 2010 Features?

  • SharePoint Features is part of Packaging and Deployment in SharePoint 2010
  • Features make it easier to activate or deactivate functionality in the course of a deployment, and administrators can easily transform the template or definition of a site by simply toggling a particular Feature on or off in the user interface.
  • Features are stored under %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES
  • The Feature subfolder includes a Feature.xml file and any number of supporting element files.
  • Features can be installed/uninstalled using STSADM tool, SharePoint Management Shell, Or Object Model.

Understanding Feature Scope

  • Sharepoint 2010 Can be scoped to Farm, Web Application, Site Collection, and Web Site level.
  • Feature Scope id defined by Scope attribute in feature.xml
  • Sample Feature will look like this

<Feature Id=”┬ŁF62C96CF-79FD-44be-8882-E9BFBD199184″

Title=”┬ŁFeature Title”┬Ł

Description=”┬ŁFeature Description”┬Ł

Version=”┬Ł1.0.0.0″

Scope=”┬ŁSite”┬Ł

Hidden=”┬Łfalse”┬Ł>

</Feature>

Web Site scoped Feature (Scope=”┬ŁWeb”┬Ł)

  • Web Site Scoped feature can be activated at individual Web site level.
  • List Instances, Event Receivers, Custom Actions etc are the custom elements for Website scope features.
  • It can be activated by STSADM command like ” stsadm “o installfeature “name FeatureFolderName “url http://sharepointserver/site/subsite
  • Alternatively open site features page by site actions -> site settings -> modify all site settings ->site features and click activate.

Site Collection scoped Feature (Scope=”┬ŁSite”┬Ł)

  • Site Collection Scoped feature can be activated at the site collection level.
  • It can contains item that can apply to Site Collection as a whole like content types that are shared across the site collection, as well as items that can be activated per site for e.g. List Instances, Event Receivers, Custom Actions etc
  • It can be activated by STSADM command like ” stsadm “o installfeature “name FeatureFolderName “url http://sharepointserver/site/sitecollectionname
  • Alternatively open site features page by site actions -> site settings -> modify all site settings ->site features and click activate.

Web Application scoped Feature (Scope=”┬ŁWebApplication”┬Ł)

  • Web Application Scoped feature can be activated at the Web application level.
  • It can contains item like administrative web application links, delegate control registrations, document forms registration etc.
  • It can be activated by STSADM command like ” stsadm “o installfeature “name FeatureFolderName “url http://sharepointserver
  • Alternatively open Manage Web Application Features from Central Administrator Central Administrator -> Application Management -> Manage Application Features and click activate.

Farm scoped Feature (Scope=”┬ŁFarm”┬Ł)

  • Farm Scoped feature can be activated at the Farm level.
  • It can contain number of element like application logic etc which can be applicable anywhere within deployment. It contacts links to /_layouts pages and files, /_admin pages etc.
  • It can be activated by STSADM command like ” stsadm “o installfeature “name FeatureFolderName
  • Alternatively open Manage Farm Features from Central Administrator Central Administrator -> Operations -> Manage Application Features and click activate.
  • Farm scope feature will be activated automatically once it is installed on server.

References

http://msdn.microsoft.com/en-us/library/ms460318.aspx

http://www.etechplanet.com/blog/understanding-scope-of-sharepoint-features-(wss-30moss-2007).aspx

Web Application Limits and Boundaries in SharePoint 2010

This article describes Microsoft SharePoint Server 2010 software boundaries and limits. These include the following:

Exceeding any of Supported limits may cause Performance Issue in SharePoint 2010.

  • Maximum size of Document in SharePoint 2010 is 2GB
  • Default Threshold of Document Size in SharePoint 2010 is 50MB
  • 250000 maximum recommended number of site collections per Web application is supported in SharePoint 2010
  • 300 Content Databases Per Web Application are supported
  • Maximum 5 Zones per Web Application. (Default, Intranet, Internet, Extranet, and Custom. Zones limit defined for farm is hard-coded.
  • Managed paths are cached on the Web server, and CPU resources are used to process incoming requests against the managed path list. Maximum 20 number of Managed Path per Web Application supported.
  • Maximum 300 MB Solution Cache Size per Web Application. The solution cache allows the InfoPath Forms service to hold solutions in cache in order to speed up retrieval of the solutions.

 

References:

http://technet.microsoft.com/en-us/library/cc262787.aspx