I've looked around and haven't found a solution yet. I have picked up code snippets here and there to find a solution.
I have a Doc Library called "Document Collaboration" with the field "Assigned To". This is a People/Groups field. These people will be able to work on a specific document(list item permission). Now, at first, they will have hidden permissions(they cant see it), but when added to the doc, they will see it and be able to contribute it, also they will get an email notification. I have attached the full code below.
So, I don't get any errors, when I go through VS10 debug. But it doesn't send any email or doesn't set the permissions. What's wrong?
using System;
using System.IO;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace ARDT.Notifications
{
/// <summary>
/// List Item Events
/// </summary>
public class Notifications : SPItemEventReceiver
{
/// <summary>
/// An item was checked in
/// </summary>
public override void ItemCheckedIn(SPItemEventProperties properties)
{
SPSite site = new SPSite("http://sp2010dev/ardt");
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Document Collaboration"];
SPListItem listItem = properties.ListItem;
SPUser userName = null;
String toAddress = null;
//EMail initializations
bool appendHtmlTag = false;
bool htmlEncode = false;
string subject = "Subject";
string message = "Message text";
//get usernames
string[] userNameArray = listItem.Fields["Assigned to"].ToString().Split(';');
for (int i = 0; i <= userNameArray.Length - 1; i++)
{
userName = web.AllUsers[userNameArray[i]];
toAddress = userName.Email;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//EMAIL USER
bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);
//PERMISSIONS
//remove permissions first
web.AllowUnsafeUpdates = true;
listItem.BreakRoleInheritance(false);
SPRoleAssignmentCollection raCollection = listItem.RoleAssignments;
//remove exisiting permissions one by one
for (int a = raCollection.Count - 1; i > -0; i--)
{
raCollection.Remove(a);
}
//grant permissions for specific list item
SPRoleDefinition roleDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
SPRoleAssignment roleAssignment = new SPRoleAssignment(userName);
roleAssignment.RoleDefinitionBindings.Add(roleDefintion);
listItem.RoleAssignments.Add(roleAssignment);
listItem.Update();
});
}
}
base.ItemCheckedIn(properties);
}
}
}