Tuesday, October 18, 2011

Get SPContext.Current.ListItem in SharePoint layouts page

I was developing a layouts page to view a list item for anonymous users with custom styles. So I was doing a little research on how to get the reference to the current list item (SPContext.Current.ListItem) so that the field values can be easily bound using SharePoint web controls itself.

It turns out to be, if a layouts page is being referenced relative to a proper web, and the List id and Item id are passed in as parameters, then I can get the SPContext.Current.ListItem referencing to the particular item being viewed and I can make use of the SharePoint Web Controls to display the field values.

http://localweb/{web url}/_layouts/ViewItem.aspx?List={list guid}&ID={itemid}

Example: http://localweb/home/_layouts/ViewItem.aspx?List={E2E0527C-EFB1-42D4-8FFE-20A6AC364845}&ID=1


In the ViewItem.aspx, I can make use of SharePoint web controls in the layouts page to display the field values as:

<SharePoint:FieldValue FieldName="Title" runat="server" />
 
Hope this helps some one else.

Thanks
Happy Coding
Senthil S

Monday, October 17, 2011

Custom Alerts in Sharepoint 2010



Alerts are automated emails generated by Sharepoint when something changes (a list item for example) and if any user has created an alert for that particular change in the list item.

In some scenarios, we may want to customize the alert email generated by the Sharepoint 2010. This can be done in two ways

1. Altering the OOTB alert template xml to use whatever the text you would like to have in the alert email
2. Adding a custom alert handler in the dll deployed to GAC and alter the alert template xml to make use of that

I personally prefer the second method since we (programmers) have more control over the email subject and body and I find it little complex to achieve my particular situation (altering the default url to link the edited/changed item).

Option 1:
To briefly describe the first option, the OOTB alert template files need to be modified. The template file can be found under {root}\TEMPLATE\XML\alerttemplate.xml. In this file you can find AlertTemplate for different list types. For my purpose I had to modify for the discussion lists (Name="SPAlertTemplateType.DiscussionBoard). Inside an AlertTemplate XML item, you will find two elements "Digest" and "Immediate" one for each type of alert. To retrieve additional fields when generating the alert message, modify "ImmediateNotificationExcludedFields" and "DigestNotificationExcludedFields" respectively and make use of those fields in the xml itself.

With that said, I have done only a basic research on the above method.

Option 2:
To have the modification done using the Second option, the above template file needs to be modified to tell the SharePoint to use a custom handler when generating alert emails.

Please COPY the original alerttemplate.xml file and work on the copy. DO NOT CHANGE THE ORIGINAL.
In the file add the reference to the custom handler that you created and deployed to GAC (I'll explain how to create the alert handler later)

Find the "Properties" node under the AlertTemplateType and add the reference like this

After updating, a powershell script needs to be run to update SharePoint to make use of the handler. Like this below.



After doing the update statement, the SharePoint services need to be restarted. To restart them, you can use the below PowerShell script
 

Thats it. After this the custom handler must be called whenever an aler email is generated based on all discussion boards

Custom Handler Creation:
Now. How to create the custom alert handler for SharePoint 2010. Below is a sample code for a simple alert handler


Hope this helps some one else.

Thanks
Senthil S

Friday, October 14, 2011

Provisioning custom list when creating a site through onet.xml

Recently, I had a situation where I need to provision a custom list which has the list template defined in the project itself. We know how to provision an OOTB list using onet.xml as explained in this msdn document

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

But, I was facing an error when I was trying to provision a custom list using a custom list definition. I added a new list definition like below.

 In the onet.xml, I had to add the below statement to provision a list instance based on the above list template.


The FeatureId is the feature id attribute of the feature in which the ListTemplate is added.

Hope this helps someone else too.

Thanks
Senthil S