Sharepoint Event Receiver Issue with AfterProperties when trying to update list item in code

The item can be updated from the SharePoint edit form, everything was working as expected. However when I tried to updated this item in a timer job, It mades my implementation went wrong. The real reason was the AfterProperties becomes Null when ItemUpdating event is called because of a code-update to list. The explanation below is from this blog: http://karthikshare.blogspot.com/2011/09/problems-with-afterproperties-in-list.html. After reading this blog, I was able to fix my issue. This blog contains the same information from Karthik’s blog.

AfterProperties will be NULL when ItemUpdating event is called because of a code-update to list:
This is something that is not documented, and lead me into a lot of trouble:

Consider this scenario:

My SharePoint list had a column named “Email”.

I wrote an “item-updating” event handler that will update a few other child lists with updated Email, when it was altered in the parent list..(to which the event handler was attached)
Here is the code:

string oldEmail = (properties.ListItem["Email"] == null) ? "" : properties.ListItem["Email"].ToString();
string newEmail = (properties.AfterProperties["Email"] == null) ? "" : properties.AfterProperties["Email"].ToString();

if(oldEmail != newEmail )
{
   //Update the other child lists with "newEmail"
}

I tested this code by editing the list item and updating the Email and the change was correctly propagated to  the other lists.

After a few days we noticed that a lot of items in those child lists, had empty email values.

Reason:

When the item in parent list is edited directly using the SharePoint interface, then both properties.ListItem[“Email”] and properties.AfterProperties[“Email”] has the expected values.

But, when the list is being updated by code, and the code does not update the “Email” (for example, the code just updates “First Name” in the list)  then properties.AfterProperties[“Email”] will be NULL in the event receiver code. AfterProperties will have the correct value, only when Email is also updated through code.

This was not the case when editing was done through SharePoint UI.( i.e If I edit that item and change only the “First Name”, then AfterProperties[“Email”] had the correct value..)

Because of this my event receiver had reset Email in all the child lists with empty values.

Resolution:

Unfortunately, the only resolution available was to check whether “AfterProperties[“Email”]” is NULL and in that case assume that the list item is updated through code somewhere else, and do nothing.

The example is not exactly what I was facing but the resolution is really suitable.

SharePoint: “Value does not fall within the expected range” exception in SPFieldMap.GetColumnNumber

When I tried to get a field value from an item, I got this exception


 System.ArgumentException Value does not fall within the expected range.
 at Microsoft.SharePoint.SPFieldMap.GetColumnNumber(String strFieldName, Boolean bThrow)
 at Microsoft.SharePoint.SPListItemCollection.GetColumnNumber(String groupName, Boolean bThrowException)
 at Microsoft.SharePoint.SPListItem.GetValue(SPField fld, Int32 columnNumber, Boolean bRaw, Boolean bThrowException)
 at Microsoft.SharePoint.SPListItem.get_Item(String fieldName)

After searching by google, I found that there are 2 main reason.

  1. First at all, you should make sure that you had included this field in ViewFields () in the query. This is my reason, I forgot to include this new field in code.
  2. Your web application’s List View Lookup Threshold reached the limitation. You have to increase this value by following steps.

Go to Central Admin > Application Management > Manage web applications > Select your web application

In the top Ribbon click on the arrow below General Settings group to display a drop-down menu, click on Resource Throttling.

untitled

Increase the value of the List View Lookup Threshold. Default value is 12 for SP 2013

2017-01-10_1509

Beside that you can also increase this value by PowerShell


$app = get-spwebapplication "http://yoursite"
$app.MaxQueryLookupFields = 20
$app.Update()

Hope it’s useful

Hiding Group Headers from SharePoint List View By jQuery

When I create a list view web part with grouping by some field, the groups will be show as this format “{FieldName}:{value of a group}({number of elements in group})”. However I just want to show “value of the current group” as a group header.

2016-11-17_1705
Group headers example

These following are what I did to hide them


$("td .ms-gb").each(function() {
var target = $(this)[0];
if (target.childNodes.length >= 3)
{
$(target.childNodes[0]).html('');
$(target.childNodes[1]).replaceWith(target.childNodes[1].nodeValue.replace(':', ''));
$(target.childNodes[2]).html('');
}
});

In this:

  • ms-gb: default class used for containing groups headers
  • childNodes[0]: <a> tag with text is {FieldName}
  • childNodes[1]: “:{value of a group}”
  • childNodes[2]: <span> tag with text is ({number of elements in group})

Enjoy!

SharePoint – Inherits in content type

Somewhen, I need to create a content type inherited from a custom content type (or OOTB content type). However, I want to remove some field from parent content type. After working hours, I realized that there is a property inherits in content type definition is defined as:

Optional Boolean. The value of this attribute determines whether the content type inherits fields from its parent content type when it is created.

If Inherits is TRUE, the child content type inherits all fields that are in the parent, including fields that users have added.

If Inherits is FALSE or absent and the parent content type is a built-in type, the child content type inherits only the fields that were in the parent content type when SharePoint Foundation was installed. The child content type does not have any fields that users have added to the parent content type.

If Inherits is FALSE or absent and the parent content type was provisioned by a sandboxed solution, the child does not inherit any fields from the parent.

https://msdn.microsoft.com/en-us/library/aa544268(v=office.15).aspx

So, to remove the Title field, for example, using the RemoveFieldRef , you must also update Inherits=”FALSE” and I always include Overwrite=”TRUE“. Like these following:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F20079481"
Name="My Content type"
Group="Custom Content types"
Inherits="FALSE"
Overwrite="TRUE"
Version="0">
<FieldRefs>
<RemoveFieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" />
</FieldRefs>
</ContentType>
</Elements>

Hope it’s helful.