Monday, June 15, 2015

Create reminder workflow for Sharepoint Online which send emails when content is outdated

On the production sites it is often needed to monitor that content of appropriate pages is kept up to date. One of the way to do it is to create workflow which will check Modified date of the page and if it is older than specified time interval, sends email to responsible users. It can be done with custom workflow created in Sharepoint Designer 2013, but it is important to use “Sharepoint 2013 workflow” as template, because “Sharepoint 2010 workflow” don’t have very useful Go to action which will be needed in order to start workflow once and run it by the loop (by default in Sharepoint Online only 2010 workflows are enabled. See Enable Sharepoint 2013 workflows in Sharepoint Online post which shows how to enable 2013 workflows in Sharepoint Online).

In order to create such workflow we need to go to Sharepoint Designer 2013 and choose Workflows > List workflow. In the opened window specify workflow name and the platform:

image

In the workflow designer first of all we need to create workflow local variable called Past with Date/Time type:

image

Then create stages with actions like shown below:

image

Let’s go through stages one by one. First stage is called Initialization. Here we subtract 12 months from current date, store result to workflow local variable called Past and go to next stage called Check content and notify. In this stage we check whether Modified date of the current list item (page) is less than Past variable (current date minus 12 month). If yes, we send email with link on the item to the specific user:

image

Adding link on the current item to notification email in workflow is little bit tricky so I will also show how link is created:

image

I.e. we need to use Current Item URL from Workflow Context for link address.

On the last stage we wait for 30 days and repeat from stage 1. As result it will be enough to start workflow once for particular page (it can be done e.g. by selecting page in default doclib view and choose Advanced > Workflows in the context menu) and it will run by itself after that without need to restart it manually each month. This is one of the advantages of having Go to action.

Enable Sharepoint 2013 workflows in Sharepoint Online (Office 365)

In Sharepoint Online you may face with the problem when will try to create workflow in Sharepoint Designer 2013: only “Sharepoint 2010 workflow” will be available in Platform Type dropdown list, while new “Sharepoint 2013 workflow” won’t be there:

image

The problem will be more unclear after you will check in admin center’s settings page that “Block Sharepoint 2013 workflows” checkbox is unchecked:

image

In order to enable “Sharepoint 2013 workflows” in Office 365 go to Site settings > Workflow settings:

image

“Workflow Health” in parenthesis is actual a link (very hidden link which looks like text. This is for sure should be fixed by MS somehow). Click on it and you will see the following page:

image

On this page click Activate button. After that when you will reconnect to the site in Sharepoint Designer you should see “Sharepoint 2013 workflow” option in the list of available platforms:

image

Please note that this configuration should be made per SPWeb. I.e. if you activated it only on the root site, it won’t be available on the sub sites. You will need to activate it on sub sites where you need Sharepoint 2013 workflow separately. Hope that this information will help you.

Friday, June 12, 2015

Fix Operation is not valid due to the current state of an object when call ashx handler in Sharepoint context

Custom ashx handler may be useful in Sharepoint e.g. when we need to return some server data in json format in order to consume it on the client side. In this case we most probably copy ashx to subfolder of 14 or 15/Layouts folder and call it in context of Sharepoint site: http://example.com/_layouts/foo/bar.ashx. If in codebehind of ashx handler we need to reopen site under elevated privileges like shown below:

   1: SPSecurity.RunWithElevatedPrivileges(
   2:     () =>
   3:         {
   4:             using (var site = new SPSite(SPContext.Current.Site.ID,
   5:                 SPContext.Current.Site.Zone))
   6:             {
   7:                 using (var web = site.OpenWeb(SPContext.Current.Web.ID))
   8:                 {
   9:                     ...
  10:                 }
  11:             }
  12:         });

we may get exception:

Operation is not valid due to the current state of an object

In order to avoid it we need to use slightly different code:

   1: var currentSite = SPContext.Current.Site;
   2: var currentWeb = SPContext.Current.Web;
   3: SPSecurity.RunWithElevatedPrivileges(
   4:     () =>
   5:         {
   6:             using (var site = new SPSite(currentSite.ID, currentSite.Zone))
   7:             {
   8:                 using (var web = site.OpenWeb(currentWeb.ID))
   9:                 {
  10:                     ...
  11:                 }
  12:             }
  13:         });

Hope that it will help someone.

Monday, June 8, 2015

Change date time fields format in Sharepoint from friendly to standard view

In Sharepoint 2013 when you create new list item or upload document to the doclib, OTB Created and Modified fields will show “A few seconds ago” text instead of actual date time (or e.g. “N minutes ago”):

image

In order to show date time in them we need to change format from Friendly to Standard mode. It is done in List settings > Field settings:

image

After that date time columns will show actual value:

image