Pages

Jan 27, 2011

Useful SPUtility tips and tricks

Here are many useful functions in SPUtility class that you may want to use in your code. When I was looking at how my custom solutions are going to be migrated to SharePoint 2010 – I noticed few obvious problems and the most obvious of them was the fact that some of the resources (3rd party license files etc) I reference by hard-coded path. Well, SPUtility has GetGenericSetupPath, a function that prefixes passed path parameter with current12 hive location. Pretty usefull if all of the sudden your code is going to run under 14 hive.

One other useful method, GetAllAuthenticatedUsers allows you to enumerate authenticated users allowed to access the web. One of the most common uses is to show list of users on a particular web as `the team` in case of a project site. The syntax is pretty basic:

SPWeb thisWeb = SPControl.GetContextWeb(Context);

string allUsers;

allUsers = SPUtility.GetAllAuthenticatedUsers(thisWeb);

writer.Write(allUsers);

SPWeb thisWeb = SPControl.GetContextWeb(Context);

string allUsers;

allUsers = SPUtility.GetAllAuthenticatedUsers(thisWeb);

writer.Write(allUsers);


Here is another useful method, EnsureSiteAdminAccess , in some cases the piece of the functionality you intend to use on the site requires administrative privileges. For example, you created an `admin` site on your portal where all of the administrative lists and other controls are available. By using EnsureSiteAdminAccess you can enforce admin access and automatically prompt a logic screen if user needs to be elevated.

I definitely recommend taking a look and making a mental note of some of the functionality available here

No comments: