Wednesday, July 22, 2009

Active Directory: How to get all related groups by user

Hi

I know this is not a Moss post and i know that in Moss we trust, but it is a nice code to have.

Recently i needed to get all related AD groups per user, so if a user A is in group B and group B is in group C, i will get a collection like so { B,C } for A.

To do so i used the following code:


/// ldapPath - Ldap://...
/// distinguishedName - The user location (CN=AAA, OU=BBB,OU=CCC,DC=DDD )
///resultField - The property field value (DisplyName, Mail)
public static void GetAllRelatedGroupsByEntry(string ldapPath, string distinguishedName,
ref List<string> groups, string resultField)
{
SearchResultCollection results = null;

using (DirectoryEntry entry = new DirectoryEntry(ldapPath))
{

using (DirectorySearcher search = new DirectorySearcher(entry))
{

search.Filter = "(&(objectClass=Group)(objectCategory=Group)(member=" + distinguishedName + "))";

results = search.FindAll();

if (results != null)
{
foreach (SearchResult result in results)
{
for (int i = 0; i <>
{
groups.Add(result.Properties[resultField][i].ToString());
GetAllRelatedGroupsByEntry(ldapPath,
result.Properties["DistinguishedName"][i].ToString(),
ref groups, resultField);
}
}
}
}
}
}

Sunday, July 12, 2009

How to add Sharepoint Workflow Activities to Toolbox

Hi

There are more workflow activities that you can use beside the one that are already in, for example "Create Task".

To add them to the toolbox you need to open visual studio and on the Toolbox section:

1. Create new tab by right click and select "Add Tab".

2. Under the new tab press right click and select "Choose Items…" (It will take a while to open).

3. Then select microsoft.sharepoint.WorkflowActions.dll, it is usually under C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI

4. Press OK.

5. Now you will have the following activities:


Monday, July 6, 2009

SPWeb.RoleDefinitions.GetByType(SPRoleType.None)

Hi all

Recently i tried to copy permission from one item to another (see How To Copy Permissions Between Site Collections) , and had a problem with role definition types that were None.
The problem occured when i tried to copy
Restricted Read role agginment.

So to solve it I used this code:

if (roleDefinition.Type != SPRoleType.None)
{
definition = web.RoleDefinitions.GetByType(roleDefinition.Type);
roleAssignment.RoleDefinitionBindings.Add(definition);
}
else
{
definition = web.RoleDefinitions.GetById(roleDefinition.Id);
roleAssignment.RoleDefinitionBindings.Add(definition);
}

Instead of geting the code by role definition type i got it by it's Id.