Creating Sites/Lists/List Items under Current User Context in SharePoint By Using SPUserToken
Creating Sites/Lists/List Items under Current User Context in SharePoint By Using SPUserToken
We might have come across the situation where we need to perform a certain action on a particular user context in MOSS 2007.
Usually we do this by performing the action under RunWithElevatedPrivileges method and updating the listitem using SPListItem.SystemUpdate() method (see here). but this approach has its own flaws like (RunWithElevatedPrivileges will run under system account, we cannot use SystemUpdate for SPSite,SPWeb,SPList, since it runs under system we will "CreatedBy" by as SystemAccount).
by using SPUserToken we can achive this without RunWithElevatedPrivileges method. All we need to do is create SPSite by passing SPUserToken of a particular user.
In below example i'm creating SPUserToken object by calling GetUserToken method.
Example:
string siteUrl = "Ur Site Url Goes Here";
string username = "YourUsername";
SPUserToken spusertoken = GetUserToken(siteUrl, username);
SPSite spsite = new SPSite(siteUrl, spusertoken);
SPWeb spweb = spsite.OpenWeb().Webs["WebName"];
SPList list = spweb.Lists["MyList"];
SPListItem item = list.Items.Add();
item["Title"] = "TestTitle";
item.Update();
spsite.OpenWeb().Dispose();
spweb.Dispose();
spsite.Dispose();
private static SPUserToken GetUserToken(string siteUrl, string userName)
{
SPUserToken userToken = null;
using (SPSite site = new SPSite(siteUrl))
{
foreach (SPUser user in site.RootWeb.AllUsers)
{
string siteUserName = user.LoginName;
if (user.LoginName.IndexOf(':') != -1)
{
string[] DomainAndUser = user.LoginName.Split(new char[] { ':' });
siteUserName = DomainAndUser[1];
}
else if (user.LoginName.IndexOf('\\') != -1)
{
string[] DomainAndUser = user.LoginName.Split(new char[] { '\\' });
siteUserName = DomainAndUser[1];
}
if (siteUserName == userName)
{
userToken = user.UserToken;
Console.WriteLine("User token found");
break;
}
}
}
return userToken;
}
Posted by
Ranjith
Google Chrome with SharePoint - First decent step for Google Chrome
Today I installed the just released Google Chrome browser and tested it with my SharePoint application.
So far it looks ok, it renders links, menus, sites and application pages correctly. but sucks while dragging and dropping the webpart in edit mode.
There is one more issue With document library, while editing the document it gave me "'Edit Document' requires a Windows SharePoint Services-compatible application and Microsot Internet Explorer 6.0 or greater." error message.
For normal browsing it works great! Its a decent release for google chrome, expecting that all above issues will be fixed in next version.
Download the Google Chrome from here
Posted by
Ranjith
How to track the changes of user contacts(colleagues) by using SharePoint API
We might have seen colleague tracker web part which keep tracks the changes made in our contacts/colleagues details like profile/lists/sites. The same tracking mechanism can be implemented by using MOSS API in any custom web part/ web control/ feature/web service...
UserProfile.GetColleagueChanges is the method which returns all changes happened with the contacts, this method also accepts a Query (UserProfileChangeQuery) where we can specify different parameters to filter the changes
I have given below the example to use GetColleagueChanges method
UserProfileManager userProfileManager = new UserProfileManager(ServerContext.Default);
UserProfile userProfile = userProfileManager.GetUserProfile("MyLoginName");
//Change the start date to get the changes from certain date
DateTime startDate = DateTime.Today;
UserProfileChangeQuery userProfileChangeQuery = new UserProfileChangeQuery(true, true);
UserProfileChangeToken userProfileChangeToken = new UserProfileChangeToken(startDate);
userProfileChangeQuery.ChangeTokenStart = userProfileChangeToken;
userProfileChangeQuery.UserProfile = true;
userProfileChangeQuery.PersonalizationSite = true;
userProfileChangeQuery.Update = true;
userProfileChangeQuery.Add = true;
userProfileChangeQuery.Delete = true;
userProfileChangeQuery.SingleValueProperty = true;
userProfileChangeQuery.MultiValueProperty = true;
userProfileChangeQuery.Colleague = true;
userProfileChangeQuery.SiteMembership = true;
userProfileChangeQuery.Anniversary = true;
UserProfileChangeDictionary userProfileChangeDictionary = userProfile.GetColleagueChanges(userProfileChangeQuery);
Dictionary
while (userProfileChangeCollection.MoveNext())
{
UserProfileChangeCollection userProfileChanges = userProfileChangeCollection.Current.Value;
foreach (UserProfileChange userProfileChange in userProfileChanges)
{
if (userProfileChange is UserProfileSingleValueChange)
{
UserProfileSingleValueChange propertyChange = (UserProfileSingleValueChange)userProfileChange;
//propertyChange will have the new and old value of profile fileds
}
else if (userProfileChange is UserProfileMultiValueChange)
{
UserProfileMultiValueChange propertyChange = (UserProfileMultiValueChange)userProfileChange;
//propertyChange will have the new and old value of profile fileds
}
else if (userProfileChange is UserProfileWebLogChange)
{
UserProfileWebLogChange listChange = (UserProfileWebLogChange)userProfileChange;
//listChange will have the newly added/modifed list item url
}
}
}
Above code will return all the changes happened in profiles/lists/sites for all contacts/colleagues....
Posted by
Ranjith
Subscribe to:
Posts (Atom)