Tuesday, March 17, 2009

Reading User Profiles from SharePoint(MOSS) programatically

User profile for SharePoint can be imported from Active directory(using master connection from current domain) and SQL server using Business data catalog(BDC) using an application definition file. Audiences can be created from the user profile information based on some rules.

To read the user profile information, we need the following namespaces.
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;
using Microsoft.SharePoint;

// GET THE current web
SPWeb web = SPContext.Current.Web;
// Get the logged in user
SPUser user = web.CurrentUser;

// Get the user context
ServerContext ctx = ServerContext.GetContext(HttpContext.Current);
// Get the user profile associated with the context
UserProfileManager userprofilemgr = new UserProfileManager(ctx);

// Get the user profile based on logged in User
UserProfile userprofile = userprofilemgr.GetUserProfile(user.LoginName);
// Reading User profile Information such as name, designation etc
string name = userprofile["First Name"];

userprofilemgr.GetUserProfile method in above code fails if the user does not have access to manage Audience. The permission to access the User profile is given by the following steps.
1. Open the SharePoint 3.0 Central Administration
2. Click on the Shared Services Provider (Farm SSP in this build) i.e. SSP linked to the current site.
3. Click on ‘Personalization services permissions’ under ‘User Profiles and My Sites’
4. Select the user ‘NT AUTHORITY\Authenticated Users’ and click on ‘Modify Permissions of Selected Users’
5. Select ‘Manage audiences’ and click on ‘Save’
6. ‘Manage Audiences’ right will be added to ‘NT AUTHORITY\Authenticated Users’

Running SharePoint custom code with elevated/ administrative privileges

Whenever writing a custom code(workflow / web parts / event handlers) for MOSS site, we sometimes require administrative previleges to run the custom code.

using Microsoft.SharePoint;

// Code to Run with admin privileges

This will take care of running the code with Administrative privileges. (Administrative account) even the user does not have access to the site.

To access the document in a SharePoint (MOSS) library programmatically

To access the document in a library, we need to do the following steps
1. First we need to create SPSite object for the site collection (E.g http://localhost)
2. Then we need to create SPWeb object inside the site collection (E.g http://localhost/testweb)
3. Get get the file by passing the relative url of the web (E.g /documents/samplefile.doc)
4. After getting the SPFile object, one can access the properties of the file as well as version.

Sample Code
using Microsoft.SharePoint;

SPSite site = new SPSite(http://localhost);
SPWeb spweb = site.OpenWeb("testweb");
SPFile file = spweb.GetFile("/documents/samplefile.doc");

Console.WriteLine(file.Url + ", " + file.Length);
SPFileVersionCollection versions = file.Versions;

for (int i = 1; i < versions.Count; i++)
SPFileVersion ver = versions[i];
Console.WriteLine(ver.VersionLabel + ", " + ver.Size);