SharePoint Sharpener

Obsessively Sharpening SharePoint

Posts Tagged ‘web part

Elevation: Run Code as an Administrator

with one comment

Sometimes you may need your web part to perform tasks for which the current user doesn’t have priviliges. For instance, we needed a sign up form for our WCM web site where the user could enter contact information that should be stored in a list.

Naturally, our web site runs with anonymous access and the the anonymous users do not have access to the underlying lists, including the list where the contact information goes.

Thus, submitting to the list is not just a matter of doing an Items.Add() because this causes a login dialogue to pop up and ultimately a 401 Unauthorized error.

Normally you’d create an element in the list with code similar to this:

image

 

However, if the logged-in user doesn’t have sufficient credentials to write to the list, a login dialogue will pop up.

 

Run with Elevation

To get around this problem you can use SPSecurity.RunWithElevatedPriviliges() like this:

image

 

For this to work, you need to instantiate the SPSite and SPWeb objects inside delegate():

image

 

Now the list will be updated with a new element, created by the system account.

Written by Thomas Sondergaard

August 27, 2008 at 6:56 am

UserProfileService Web Service Returns Multiple Instances of User Profiles with Identical Names

without comments

The lack of a built-in overview of all users in SharePoint makes it difficult to create a simple phone book of your company’s employees. The object UserProfileManager would be the obvious starting point if you were to create your own phone book for SharePoint – and many have tried this. Google this object and you’ll find that the only real way to query all users effectively is using the built-in web services of MOSS.

Specifically, UserProfileService (http://server/_vti_bin/userprofileservice.asmx) is ideal for querying user profiles from e.g. within a web part.

In Visual Studio, when you’ve set up the web service, you connect to the web services like so:

image

Then, you can query the number of profiles in the user profile database by calling the method GetUserProfileCount:

image

Knowing the number of user profiles, you can iterate through them and pull the user data:

image

Some names show up several times

You will find, however, that GetUserProfileByIndex only holds information about users whom have active MySites. Strangely enough, even if the total number of users exceed the number of users with MySites, you can still iterate through all the user profiles you found with GetUserProfileCount.

Say, you have the following people in your User Profile database:

1 – Ted Pattison
2 – Liam Cleary
3 – Amanda Murphy
4 – Yvonne Harryman
5 – Steve Pietrek

Let’s assume that Amanda and Yvonne don’t have MySites. Then the output of the above loop would look like this:

1 – Ted Pattison
2 – Liam Cleary
3 – Liam Cleary
4 – Liam Cleary
5 – Steve Pietrek

GetUserProfileByIndex will not fail when you loop through 5 profiles because that is the number of profiles present in the database. On the other hand, querying a profile that doesn’t have a MySite, e.g. GetUserProfileByIndex(3) and GetUserProfileByIndex(4), will return the latest user with a MySite, in this case Liam Cleary (index no. 2).

Solution

In order to avoid this situation the value stored in NextValue can be used. NextValue contains the index of the next user with a valid MySite user profile. Looking at the first table again, the NextValue values are shown in parentheses:

1 – Ted Pattison (2)
2 – Liam Cleary (5)
3 – Amanda Murphy (5)
4 – Yvonne Harryman (5)
5 – Steve Pietrek ()

The following line can then be added to the loop to ensure that the next profile to be shown contains usable information:

image

Now you just need to output all the information in an SPGridView and the phone book is ready to be implemented.

Written by Thomas Sondergaard

August 8, 2008 at 4:23 pm

Posted in Development

Tagged with , ,