Thursday, August 27, 2009

How to get the users of a Active Directory group in SharePoint.

While adding a new user to a SharePoint group, you can enter Active Directory group name, so that all the users inside the AD group can access the SharePoint site, which is completely a out-of-box functionality in SharePoint. But there might be requirement where we need to access users of a specific AD group independently to perform some action against each of them.

Below is the code snippet in c# to acces each users details of a specific Active Directory group.

NameSpace: using Microsoft.SharePoint.Utilities;

bool reachMaxCount;


// objSPWeb is the SPWeb object
SPPrincipalInfo[] adGroupUsers = SPUtility.GetPrincipalsInGroup(objSPWeb, "DomainName\\GroupName", 100, out reachMaxCount);


foreach (SPPrincipalInfo objAdUserInfo in adGroupUsers)
{
      string loginName = objAdUserInfo.LoginName;
      string displayName = objAdUserInfo.DisplayName;
      string emailId = objAdUserInfo.Email;
}

**************Fetch the AD users using LDAP**************


public void GetADUsersInADGroup(string domainName,string groupName)

{

string userNames = "Users : " + Environment.NewLine;


string anyADUserID = "samarendra";

string anyADUserPassword = "password";
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domainName, anyADUserID, anyADUserPassword);


DirectorySearcher dSearch = new DirectorySearcher(entry);

dSearch.Filter = "(&(objectClass=group)(cn=" + groupName + "))";

SearchResult results = dSearch.FindOne();
if (results != null)
{

DirectoryEntry objGroup = new DirectoryEntry(results.Path, anyADUserID, anyADUserPassword, AuthenticationTypes.Secure);


System.DirectoryServices.PropertyCollection pcoll = objGroup.Properties;

for (int count = 0; count < pcoll["member"].Count; count++)

{

DirectoryEntry deUser = new DirectoryEntry("LDAP://" + domainName + "/" + pcoll["member"][count].ToString(), anyADUserID, anyADUserPassword, AuthenticationTypes.Secure);


userNames += deUser.Properties["cn"].Value + Environment.NewLine; // cn is for login Name

}


objGroup.Close();

}

}


Samarendra Swain

Wednesday, August 26, 2009

Adding/Customize action menu in SharePoint

A common requirement that every Sharepoint developer is asked for today is customization of the SharePoint site based on various customer specific requirements. Most often customers have requirements where they want to add some additional features to action menus like Site action menu, ECB menu or List/library Action menu, etc. To achieve this, we have to write custom action in XML and consume it within the site as a new feature. In Sharepoint if we need to integrate new feature for a List / Library, we can achieve this easily by creating a custom action feature.


1. Create a folder named TestCustomAction under C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES directory.


2. Next, create a file named Feature.xml in the same folder with the following codes.

Enlarge the image

3. Next, create another file, TestCustomActions.xml in the same folder with the following codes.


Enlarge the image

4. Create a page named TestCustomActions.aspx under layouts folder and add your own Business Logic.


5. Finally, deploy this feature by running the command in the command prompt.


To activate this feature: stsadm -o installfeature -filename TestCustomAction\feature.xml -url http://TestServer/Site.

Finally to see the changes in the site do an iisreset.

See here also:Adding/Customizing action menu in SharePoint

Samarendra Swain

Tuesday, August 25, 2009

Create custom property in a web part as a dropdown list

SharePoint allows you to create custom web parts to help you provide highly customized solutions to your clients/users. The properties of these web parts are added by default but to further customize the solution, you can also create custom properties. The following example demonstrates how to add a custom dropdown property to a custom webpart.
// Create a Enum for Languages

public enum LanguageByEnum

{
English = 0,
French,
Spanish
};

// Create Get/Set for the dropdown property

protected LanguageByEnum languageName;

[Personalizable(PersonalizationScope.User),
Category("Select Language"),
WebBrowsable, WebDisplayName("Select language:")]
public LanguageByEnum SelectedLanguage
{
get { return languageName; }
set { languageName = value; }
}

I have written the same here also:Create custom property in a web part as a dropdown list

Samarendra Swain

Create SharePoint Web Application Programmatically

Generally web applications in SharePoint are created using the out-of-box functionality provided within Central Administration. But because of some client's needs there might be a requirement of creating these web applications within a custom utility solution. This functionality can be achieved programmatically using Microsoft SharePoint APIs. By this, one can easily create a web application & associate a content database to it, register the web application in the IIS server, create an associated application pool and define different credentials for the web aplication.

Example code using C#.Net

// namespace required
using System.IO;
using Microsoft.SharePoint.Administration;


// Create new object of SPWebApplication & SPWebApplicationBuilder class
SPWebApplication newApplication;
SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);

// Set necessary values to webAppBuilder object

webAppBuilder.Port = portNo;
webAppBuilder.RootDirectory = new DirectoryInfo(@"D:\projects\TestWebSite3030");
webAppBuilder.ApplicationPoolId = appPoolId;
webAppBuilder.ApplicationPoolUsername = appPoolUserName;
webAppBuilder.ApplicationPoolPassword = password;
webAppBuilder.CreateNewDatabase = true;
webAppBuilder.DatabaseServer = dbServerName; // DB server name
webAppBuilder.DatabaseName = dbName;// DB Name
if (isSQLAuth)
{
webAppBuilder.DatabaseUsername = dbUid; // dbUid is username of the DB sever
webAppBuilder.DatabasePassword = dbPwd; // dbpassword is password of the DB sever
}
webAppBuilder.UseNTLMExclusively = true; // authentication provider for NTLM
webAppBuilder.AllowAnonymousAccess = isAnonymous; // anonymous access permission

// Finally create web application
newApplication = webAppBuilder.Create();
newApplication.Provision();


I have written the same here also:Create SharePoint Web Application Programmatically

Samarendra Swain

Enable publishing feature programmatically in MOSS 2007 site

In SharePoint, integrating publishing feature is possible only in MOSS 2007 site and not in WSS site.By turning on publishing feature you can use Web Content Management(WCM) feature in the site.Once the WCM feature is enabled you can then brand the site with your custom look and feel and can also enable the multilingual support in the site. The multilingual feature helps in creating different folder-based sites according to the languages set to the site.

While creating a site collection or site & workspaces using SharePoint API , you can add these two lines code to enable the publishing feature.Provided below is an example using C# for enabling publishing feature at site collection level.

Code in C#

// Turn on publishing feature
// objSiteCollection is the Site Collection object
objSiteCollection.Features.Add(SPFarm.Local.FeatureDefinitions["PublishingSite"].Id);

// Make the necessary changes to the local server to use the feature
SPFarm.Local.FeatureDefinitions["PublishingSite"].Provision();


I have written the same here also:Enable publishing feature programmatically in MOSS 2007 site


Samarendra Swain

Monday, August 24, 2009

Programmatically assign specific permission in SharePoint

SharePoint site security helps manage permissions for different resources within a site by defining the levels of accessibility permissions for different peoples and groups. In SharePoint, always the top-level or the parent level permissions are inherited to it's child contents (e.g. a sub-site inheriting permissions from it's parent site collection).

In-order to create unique permission we need to break the inheriting parent permission and create new permission level for the SharePoint content. These permissions can be defined for specific users or groups.

Provided below is an example using C# for defining custom permission for a list item.

// assign a item to SPListItem object.
SPListItem objLstitem = objLst.Items[0]; // objLst is the SPList object


// get the user by ID/Email
// objSpWeb is the SpWeb object


SPUser objUser = objSpWeb.SiteUsers.GetByEmail(samarendra@test.com);


// Break inheriting parent permissions for this List Item.
objLstitem.BreakRoleInheritance(false);


// assign Role to the defined User
SPRoleDefinitionCollection objWebRoleDefn = objSpWeb.RoleDefinitions;
SPRoleAssignment objRoleAssign = new SPRoleAssignment(objUser);


// specify the name of the role definition like [Full Control][Read][Contribute] etc.
objRoleAssign.RoleDefinitionBindings.Add(objWebRoleDefn["Contribute"]);
objLstitem.RoleAssignments.Add(objRoleAssign);


I have written the same here also:Programmatically assign specific permission in SharePoint

Samarendra Swain