Thursday, September 25, 2014

Resolve “The property does not exist on type 'SP.ListItem'. Make sure to only use property names that are defined by the type” error when posting field values using SharePoint REST api

When I tried to update field data of a document in a document library using REST api I got Bad Request (400) error message. Surprisingly this was the same code which is working on my office 365 environment. Following is the full error message

The property 'ContentTypeId' does not exist on type 'SP.ListItem'. Make sure to only use property names that are defined by the type

Following is the code I used to update the document

  1. var updateUrl = "http://sp13/sites/dev/_api/Web/GetFileByServerRelativeUrl('/Sites/dev/Lists/DOA/Test/CSM.docx')/ListItemAllFields";
  2.  
  3. $.ajax(
  4. {
  5.    'url': updateUrl,
  6.    'method': 'POST',
  7.    'data': JSON.stringify({
  8.        '__metadata': { 'type': 'SP.ListItem' },
  9.        'ContentTypeId': '0x0101005C9775F3130F7A498C5C8CF69C1750AE03'
  10.    }),
  11.    'headers': {
  12.        'accept': 'application/json;odata=verbose',
  13.        'content-type': 'application/json;odata=verbose',
  14.        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
  15.        'X-Http-Method': 'MERGE',
  16.        "If-Match": "*"
  17.    },
  18.    'success': function (data) {
  19.        alert('success');
  20.    },
  21.    'error': function (err) {
  22.        alert('error');
  23.    }
  24.   }
  25. );

The reason for above error is that the type attribute is not compatible in this scenario. I need to provide the exact entity name instead of generic SP.ListItem.

How can I find the entity name for the list? it is very easy. I just need to provide following request.

http://sp13/Sites/dev/_api/lists/getbytitle('DOA')

Following is the entity name that I should use in my code

image

Following is the modified code that works on my on premises environment as well

  1. var updateUrl = "http://sp13/sites/dev/_api/Web/GetFileByServerRelativeUrl('/Sites/dev/Lists/DOA/Test/CSM.docx')/ListItemAllFields";
  2.  
  3. $.ajax(
  4. {
  5.    'url': updateUrl,
  6.    'method': 'POST',
  7.    'data': JSON.stringify({
  8.        '__metadata': { 'type': 'SP.Data.DOAListItem' },
  9.        'ContentTypeId': '0x0101005C9775F3130F7A498C5C8CF69C1750AE03'
  10.    }),
  11.    'headers': {
  12.        'accept': 'application/json;odata=verbose',
  13.        'content-type': 'application/json;odata=verbose',
  14.        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
  15.        'X-Http-Method': 'MERGE',
  16.        "If-Match": "*"
  17.    },
  18.    'success': function (data) {
  19.        alert('success');
  20.    },
  21.    'error': function (err) {
  22.        alert('error');
  23.    }
  24.   }
  25. );

Friday, September 19, 2014

Get “Multiple lines of text” field value using SharePoint 2013 REST services

If we get list item from SharePoint REST services, we may get unexpected results for “Multiple lines of text” fields. Let’s take the scenario given below. I used following service to get results.
https://sp13/sites/dev/_api/web/lists/getbytitle('r')/items(4)
Instead of the field value we may get a escaped html string like below.
image
The reason for this behavior is that the column is enabled for rich text. If we want to keep the rich text input, we may need to escape the string using RegEx for some JavaScript functionality. For an example we can use following script to display expected comment in a textbox.
tmpComment = data.d.AGMComment != null ? data.d.AGMComment.replace("'", "") : "";
$('#txtApprovalComments').val($(tmpComment).text());
But if we don’t need the rich text behavior we can simply make the column to accept only plain text.
image
After modifying column settings as shown above, following will be the output from the same service call
image

Wednesday, September 17, 2014

SharePoint 2013 Visual Studio based workflow - customize task approval email to include link to respective task

In SharePoint 2013 workflow architecture is drastically changed when compared with SharePoint 2010. Some new activities like “HttpSend” are introduced to the platform and some activities are improved a lot. On the other hand some scenarios became very hard to implement.

Let’s assume that we have a SharePoint 2013 Visual Studio based workflow and need to assign a task to a user. We expect the notification email to contain the link to respective task. isn’t it ?

In SharePoint 2010 it was very easy to include the task url in the mail. But that is not the case anymore. It’s not possible to include the task url in the default mail. Instead it is sent like below which is not user friendly.

image

In this post I’ll show a workaround to resolve the problem. Following are the steps we need to perform

1. Add “SingleTask” activity and configure necessary parameters

image

2. Modify WaitForTaskCompletion property to false

This will avoid the “SingleTask” activity to wait until the approval. Furthermore change “WaiveAssignmentEmail” property value to “Yes” which avoids the notification email.

image

3. Compose an email manually

In this email I’ll include a link to assigned task. To get the guid of task list, I will use “GetTaskListId” activity. Furthermore I’ll get the task id from “SingleTask” activity output.

From those elements I’ll construct the url of the assigned task

e.g.: “http://sp13/sites/dev/DOAApprovalForm.aspx?List="+taskListId.ToString()+"&ID="+taskItemId+"

Then I’ll construct the email body as I wish

  1. "<html><body style='font-size:11pt;font-family:Segoe UI Light,sans-serif;color:#444444;'><div>Contract comment is : " + contractComment+ " </br>Please approve this <a href="+siteUrl + "SitePages/DOAApprovalForm.aspx?List="+taskListId.ToString()+"&ID="+taskItemId+">Task</a></div></body></html>"

 

4. Handle Approve or Reject actions using a “Pick” activity

We will use “WaitForFieldChange” activity to pause the workflow until user presses Approve or Reject buttons. Since there are two possible values in the pausing field we need to use a “Pick” activity

image 

5. As mentioned earlier, we will use “WaitForFieldChange” activities on both branches, configured for “TaskOutcome” field.

image

For the first “WaitForFieldChange” activity, the FieldValue is set to “Approved” and for the other one it is “Rejected”

6. I’ll update the taskOutcome with respective values to continue the workflow

image

This will allow us to modify email as we wish and pause the workflow until the task is approved or rejected

image

Tuesday, September 2, 2014

Open a div in SharePoint modal dialog

Sometimes we need to load a div in modal popup instead of a page. Following is an example code that we can use in such scenarios.

In this scenario I use a modal popup to load file upload control in the same page.

  1. function Opendialog(ctType) {
  2.   SP.SOD.executeOrDelayUntilScriptLoaded(function () {
  3.     var element = document.createElement('div');
  4.     element.innerHTML = '<input type="file" id="documentUpload" accept="*" /><input id="btnUpload" type="button" value="Upload" onclick="UploadFile(\'' + ctType + '\')"; />';
  5.  
  6.     SP.UI.ModalDialog.showModalDialog({
  7.      html: element,
  8.      title: 'Document Upload',
  9.      allowMaximize: false,
  10.      showClose: true,
  11.      dialogReturnValueCallback : Function.createDelegate(null, CloseCallback),
  12.      autoSize: true
  13.     });
  14.    }, "SP.js");
  15. }
  16.  
  17. function CloseCallback(result, target) {
  18.    location.reload(true);
  19. }