Thursday, February 4, 2016

Sharepoint Update vs Systemupdate vs UpdateOverwriteVersion

When creating event receivers or workflows it might be interesting to look at the differences between the following SPListItem methods. The differences might be subtle but it can make a huge difference when you have extra event receivers or workflows attached to your SharePoint list or items.

Update()

  • Updates the item in the database.
  • Updates the “Modified” and “Modified by” values.
  • Creates a new version

Systemupdate()

  • Updates the item in the database.
  • No changes in the “Modified” and “Modified By” fields.
  • No new version.
  • Triggers the item events.

Systemupdate(true)

  • Same as Systemupdate() and increments the item version.
  • Using SystemUpdate(false) is exactly the same as SystemUpdate()

UpdateOverwriteVersion()

  • Updates the item but does not create a new version.
  • Updates the “Modified” and “Modified by” values.
Note that :
You can also disable the triggering of events by using “this.EventFiringEnabled = false;”. Do your update and enable the events again with “this.EventFiringEnabled = true;”

Tuesday, February 2, 2016

Formatting a Date in Your Display Template

Date formatting in SharePoint is a subject discussed over and over in all communities, forums, blogs, etc. Whether it is some discrepency in daylight savings time output, region settings or just simply to render the date in a format that fits your design implementation guidelines.
I'll be discussing the latter - using SharePoint 2013's content by search web part and display templates.

In the following sample, we have an article displayed with 3 very common components - Date, Title and Description. As you can see, the date has a specific format that needs to be followed.
During the process of creating the article the date will be selected by the content author using a date field like so.
 

SharePoint by default takes the value and formats the date. So in search results it renders like this...
Wednesday, November 5, 2014
To control the output for the date that you want, you will have to add a couple of variables to your display template. The first variable (which I'm calling parseDate) takes the inputValue, that is stored in the database as a text string object, and parses the value of the field back to a datetime object. The second variable does the actual formatting.
In your display template you will see sections of javascript code enclosed in tags like these - you will see where all the initial variables are set...this is where you will be adding the variables.

<!--#_
 
_#-->

And here's the code for copying.
var parseDate = new Date(line3.inputValue);
var line3Date = parseDate.format('MM/dd/yyyy');
Finally - anywhere you want to render the newly formatted date you will add this snippet.
_#= line3Date =#_
Refer to http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.95).aspx for more information on the specific date formatting rules that you can use.