1. Open
Visual Studio & Select Event Receiver & give proper Name.
2. After
giving the Proper Name to the Project, the next screen appears asking to deploy
the solution as Sandboxed or Farm level. Select Deploy as Farm Solution
3. The Next Popup will provide to choose the Event
Receiver Settings. Here in the Type of Event Receiver we will select “List Item
Event” as I have to fire the Event Receiver on each item. Then select the
“Document Library” as the Event Source & finally the when the event is to
be fired, here select “An item was updated” as it is a document library.
If we want to achieve this functionality
“List” then we need to fire the Event Receiver on “An Item was Added” as it is
a list. Click Finish.
4. The Default solution will look as below
5. The default code in the Event receiver file will
be as below
6. Now in the ManageSharedMembers.cs file add the
below code.
public class ManageSharedMembers
: SPItemEventReceiver
{
private string
listname = "";
//Give Shared
Members Column Name
private string
SharedMembers = ConfigurationManager.AppSettings["DocumentSharedMembers"];
//Give All Shared
Members Column Name
private string
DestinationSharedMembers = ConfigurationManager.AppSettings["DocumentDestinationSharedMembers"];
/// <summary>
/// An item was
updated.
/// </summary>
public override
void ItemUpdated(SPItemEventProperties
properties)
{
listname = properties.ListTitle.ToString();
//Disable Event Firing as we are updating the
item
this.EventFiringEnabled = false;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//Get current
item
SPListItem currentitem =
properties.ListItem;
//Check for the Shared Members & All
Shared Members fields are present or not
if
(properties.ListItem.Fields.ContainsField(SharedMembers) &&
properties.ListItem.Fields.ContainsField(DestinationSharedMembers))
{
properties.Web.Site.AllowUnsafeUpdates = true;
//Check whether the Shared Members column
contains value or not.
//If the event receiver is fired on List
then in the if condition check the shared members column for empty or not as
below commented
Properties.AfterProperties[SharedMembers].ToString()!=string.Empty
If it is on document library then as shown
in below if condition
if
(currentitem.Properties[SharedMembers].ToString() != string.Empty)
{
string fieldValue =
currentitem[SharedMembers].ToString();
SPFieldUserValueCollection
users = new SPFieldUserValueCollection(currentitem.Web,
fieldValue);
SPFieldUserValueCollection
user = new SPFieldUserValueCollection();
//Create LDAP connection with the Active
Directory Server
DirectoryEntry de
= new DirectoryEntry();
de.Path = "LDAP://Network
IP/DC=sharepoint,DC=com";
de.AuthenticationType = AuthenticationTypes.Secure;
//Read users
one by one to check for any groups present
for (int
i = 0; i < users.Count; i++)
{
SPFieldUserValue singlevalue
= users[i];
SPUser singleuser =
singlevalue.User;
string groupname =
singleuser.Name.ToString();
//Check whether the given value is user
or group
if
(singlevalue.User.IsDomainGroup)
{
//If given
value is a group then get all users from AD of that group
DirectorySearcher
searcher = new DirectorySearcher();
searcher.Filter = ("(&(objectClass=group)(CN=" +
groupname + "))");
SearchResult
result = searcher.FindOne();
string username;
if (result != null)
{
DirectoryEntry
deGroup = new DirectoryEntry(result.Path);
PropertyCollection
pcoll = deGroup.Properties;
try
{
//Fetch the users from the AD and add
in the SPFieldUserValueCollection
foreach (object obj in
deGroup.Properties["member"])
{
username = obj.ToString();
char[] split1 = { ',' };
string[] getname =
username.Split(split1);
string temp = getname[0];
char[] split2 = { '=' };
string[] getname2 =
temp.Split(split2);
username = getname2[1];
SPUser uservalue =
currentitem.Web.EnsureUser(username);
user.Add(new
SPFieldUserValue(currentitem.Web,
uservalue.ID, uservalue.Name));
}
}
catch (Exception)
{
}
}
}
else
{
//if
singlevalue is user then you can use all SPUser properties & add.
SPUser userdetails =
singlevalue.User;
user.Add(new
SPFieldUserValue(currentitem.Web,
userdetails.ID, userdetails.Name));
}
}
//update all these users in the All
Shared Members column.
currentitem[DestinationSharedMembers] =
user;
currentitem.Update();
//After
updating the item now enable the Event Firing
this.EventFiringEnabled = true;
}
}
//}
});
}
catch (Exception)
{
}
base.ItemUpdated(properties);
}
}
Now Build & deploy the solution in the SharePoint Web
Application.
When this Event Receiver is fired, it will check for any
groups in the “Shared Members” columns & then fetch the corresponding users
from the AD & store them in the other column i.e. “All Shared Members”.