Archive for February, 2011


This post explains how you can fins the list of users who are having permissions to a particular list or a list item. Also in case if a group is directly assigned permissions we can loop through the group to find each users of that group too.

We can access the permission details of a List  or a ListItem  using the properties

  • List.RoleAssignments
  • ListItem.RoleAssignments

Below code explains how to get SPUser details form the RoleAssignment property.

    private void AddListRoleAssignmentNodes(SPList objList)
    {   
      if (objList.HasUniqueRoleAssignments)
      {
         SPRoleAssignmentCollection oRoleAssignments =
            objList.RoleAssignments;

         foreach (SPRoleAssignment oRoleAssignment in oRoleAssignments)
         {
            SPPrincipal oPrincipal = oRoleAssignment.Member;
            try
            {
               // Retrieve users having explicit permissions on the list
               SPUser oRoleUser = (SPUser)oPrincipal;
            }
            catch (Exception ex)
            {
               string msg = ex.Message;
            }
            try
            {
               // Retrieve user groups having permissions on the list
               SPGroup oRoleGroup = (SPGroup)oPrincipal;

               if (oRoleGroup.Users.Count > 0)
               { 
                  string strGroupName = oRoleGroup.Name;
                  // Add code here to retrieve Users inside this User-Group
               }
            }
            catch (Exception ex)
            {
               string msg = ex.Message;
            }
         }
      }
   }

The first try catch gets all the users and second one gets all the groups that are having permissions on the list/list item.

Advertisements

Please note this is a post by Albert Meerscheidt in msdn blogs.  I am copying this as is just not to miss this wonderful post in future.

Problem: There are issues with particular field names like ItemName which will be truncated in an alert email at 70 characters. There can also be situations where you want to embed additional content in the email or change the layout and email appearance altogether.

Solution: Make use of the IAlertNotifyHandler interface to intercept the email and modify it.

We can create our own class that inherits from the IAlertNotifyHandler interface and uses theOnNotification method. This will allow you to intercept the outgoing alert emails and modify them. We can access most of the properties for the alert and with some xml parsing and SharePoint object model code, we can extract all the information we need to build up the email. We can then construct the HTML stub to display the email based on your requirements and send the email out using SharePoint’s SendMail functionality.

Steps:

I have included the sample code below along with the steps to set up the scenario. I have formatted the output of my code to resemble the default alert template emails as close as possible, you can customize it further to suit your needs.

1      Create a class project that inhertits from the IAlertNotifyHandler interface. Include the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces in the project.

This is the code for the class:

public class Class1:IAlertNotifyHandler

{

#region IAlertNotifyHandler Members

public bool OnNotification(SPAlertHandlerParams ahp)

{

try

{

SPSite site = new SPSite(ahp.siteUrl+ahp.webUrl);

SPWeb web = site.OpenWeb();

SPList list=web.Lists[ahp.a.ListID];

SPListItem item = list.GetItemById(ahp.eventData[0].itemId) ;

string FullPath=HttpUtility.UrlPathEncode(ahp.siteUrl+”/”+ahp.webUrl+”/”+list.Title+”/”+item.Name);

string ListPath = HttpUtility.UrlPathEncode(ahp.siteUrl + “/” + ahp.webUrl + “/” + list.Title);

string webPath=HttpUtility.UrlPathEncode(ahp.siteUrl+”/”+ahp.webUrl);

string build = “”;

if (ahp.eventData[0].eventType==1)

eventType=”Added”;

else if(ahp.eventData[0].eventType==2)

eventType=”Changed”;

else if(ahp.eventData[0].eventType==3)

eventType=”Deleted”;

build = “<style type=\”text/css\”>.style1 {              font-size: small; border: 1px solid #000000;”+

“background-color: #DEE7FE;}.style2 {               border: 1px solid #000000;}</style></head>”+

“<p><strong>”+ item.Name.ToString() +”</strong> has been “+eventType +”</p>”+

“<table style=\”width: 100%\” class=\”style2\”><tr><td style=\”width: 25%\” class=\”style1\”>”+

“<a href=”+ webPath +”/_layouts/mysubs.aspx>Modify my Settings</a></td>”+

“<td style=\”width: 25%\” class=\”style1\”> <a href=”+ FullPath +”>View “+item.Name+”</a></td>”+

“<td style=\”width: 25%\” class=\”style1\”><a href=” + ListPath + “>View ” + list.Title + “</a></td>” +

”        </tr></table>”;

string subject=list.Title.ToString() ;

SPUtility.SendEmail(web,true , false, ahp.headers[“to”].ToString(), subject,build);

return false;

}

catch (System.Exception ex)

{

return false;

}

}

#endregion

}

2.            GAC the dll.

3.            Make a copy of the alertTemplates.xml file found at this location: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\XML. Always work with a copy of AlertTemplates.xml, not the original.

4.            Call this new file CustomAlertTemplates and save the file. Edit the file and search for the keyword properties:

Include these additional lines into the properties block:

<NotificationHandlerAssembly>AlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>

<NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>

<NotificationHandlerProperties></NotificationHandlerProperties>

The entire stub should look like this now:

<Properties>

<ImmediateNotificationExcludedFields>ID;Author;Editor;Modified_x0020_By;Created_x0020_By;_UIVersionString;ContentType;TaskGroup;IsCurrent;Attachments;NumComments;</ImmediateNotificationExcludedFields>

<DigestNotificationExcludedFields>ID;Author;Editor;Modified_x0020_By;Created_x0020_By;_UIVersionString;ContentType;TaskGroup;IsCurrent;Attachments;NumComments;</DigestNotificationExcludedFields>

<NotificationHandlerAssembly>AlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>

<NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>

<NotificationHandlerProperties></NotificationHandlerProperties>

</Properties>

Include this xml stub in each alert template section you want in the alert template file.

5.            Run this command from C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN:  stsadm -o updatealerttemplates -filename “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\XML\customalerttemplates.xml” -url <your sharepoint site url>

6.            Run this command:  stsadm -o setproperty -pn job-immediate-alerts -pv “every 1 minutes” so that you can see the log file come back in one minute. Be sure to set the time back after testing.

7.            Make sure you have SharePoint already configured for outgoing emails.

8.            Make sure that you have alerts for the document library turned on if you are testing with the document library.

9.            Run this command from the command prompt: iisreset

10.         Run this command from the command prompt: services.msc

11.         From the services window, restart the Windows SharePoint Services Timer

.

Actual Post Link : http://blogs.msdn.com/b/sharepointdeveloperdocs/archive/2007/12/14/how-to-customizing-alert-emails-using-ialertnotificationhandler.aspx


There are various reasons this error might come up. In my case I ended up with this error when i tried to add an item in to a list in a site which has Anonymous access enabled.

I just got an error “Unexpected error occured” & the browser stayed in the newform.aspx page. But when I checked the list new item is being created in the list.

So there is no issue with the permissions. And I came to know that “Anonymous users cannot start a workflow or an event handler“.

Then I noticed there was a feature which I have written for a custom list. I deactivated that and it worked well..

PS : But still there must be a way to start a workflow/event handler by an anonymous user. If any one find so, Please post.