How to preserve user metadata in SharePoint when using System Update in SharePoint object model API

2. April 2009

There are two primary ways exist to create SPSite as a security context. One way is to use the current Windows or Forms identity, which is the default method whether you are accessing the site from the WSS Web application or an administrative console. This is also the method used with the SPSecurity.RunWithElevatedPrivileges delegate-the current principal that happens to be SHAREPOINT\system is used to create the site security context.

The other way to create the SPSite is by using an SPUserToken object. The SPUserToken is the token created upon authentication. It references the principal of the user from either Active Directory or the identity store with its groups and roles. In the case of a Windows identity, this token is used to query Active Directory for the TokenGroups property. These tokens time out after 24 hours, making them a good candidate for system code that needs to impersonate users in the case of workflow actions or post-processing of list data that happens slightly after the original action (not days later). This token timeout value can be set by using the STSADM console. Using the user token in the constructor of the SPSite enables code to make changes to the WSS object model just as if the actual user were making the changes. Using impersonation is a security-sensitive operation that requires the SharePointPermission with the Impersonate property set to true.

You can request the token for any user in the system by using the UserToken property of the SPUser class (provided that your code has the SharePointPermission with the Impersonate rights). If the current user is not the user requested, WSS builds the token on the fly from the user's Security ID and group membership. You can then pass this token to the SPSite's contructor to create a new impersonated security context.

For example, let's revisit the list event receiver from Chapter 6, "Lists and Content Types." On creation of a Litware Company record in either the Vendor or Customer list, we will create an announcement with the credentials of the user who created the item. When this code runs, it is in the context of the SHAREPOINT\system account, and we don't have access to the actual credentials of the user who created the item. To create the item under the impersonated security context, simply obtain a user token from the SPUser profile that created the object and pass that into the SPSite constructor. When the item is inserted into the announcements list, it will be as if the impersonated user created the item even though the event receiver is running under the identity of SHAREPOINT\system.

public override void ItemAdded(SPItemEventProperties properties) {
DisableEventFiring();
string CompanyName = properties.ListItem["Company"].ToString();
properties.ListItem["Company"] = FormatStringValue(CompanyName);
properties.ListItem.Update();
SPUserToken token =
properties.OpenWeb().AllUsers[properties.UserLoginName].UserToken;
using( SPSite site = new SPSite(properties.SiteId, token) )
{
using(SPWeb web = site.OpenWeb(properties.WebUrl))
{
SPListItem announcement = web.Lists["Announcements"].Items.Add();
announcement["Title"] = properties.ListItem["Company"].ToString();
announcement["Body"] = "A new company was added!";
}
}

In this code sample, we are using the AllUsers property of the site. Users are available through a reference to the site (the SPWeb class). Three user collections are available within the site, and choosing which one to use may be confusing. The AllUsers property lists all users, including members of the site as well as members of domain groups that are members of the site. The SiteUsers property contains the users who are members of the site collection, and the Users property is the smallest group, containing only users who are explicitly members of the site.

SharePoint

Comments

2/16/2010 3:01:30 PM #
Hi just came across your site and have been reading some of your posts and just wondering why you selected a IIS hosting dont you find it difficult to do anything with?
2/23/2010 10:04:20 AM #
A currency will lose value if there is a high level of inflation in the country or if inflation levels are perceived to be rising.
3/1/2010 11:39:38 PM #
Thanks for that, really interesting on the subject of How to preserve user metadata in SharePoint when using System Update in SharePoint object model API. I will do some more googleing for How to preserve user metadata in SharePoint when using System Update in SharePoint object model API
3/2/2010 3:59:07 PM #
It is important that you be extremely cautious when browsing the internet. Avoid settling for just any “buy Viagra cheap deal”. Instead, emphasis should be paid on the caliber and credibility of the online pharmacy. With the success of Viagra tablets many fraudsters have attempted to duplicate false equivalents.
3/3/2010 7:59:47 PM #
Hey there, are you having issues with your hosting? I needed to refresh the page about four times to your site load. You might want to change hosts, or could the problem be from me?

-Bobby
3/11/2010 12:13:41 AM #
I hapen to agree with John above.  I will find the information and post it here.  We'll have the FACTS momentarily.
3/22/2010 9:58:04 PM #
Thankyou for all your assistance.. bookmarked this blog..
3/26/2010 6:58:38 AM #
Fantastic stuff you've got here. It's a real treat to read.
3/26/2010 11:20:54 PM #
I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites weblog list and will be checking back soon. Please check out my site as well and let me know what you think.
3/28/2010 2:09:38 AM #
Thanks for that marvelous blog posting! I really enjoyed reading it, you are a great writer.  I actually added your blog to my favorites and come back soon. Keep up the exciting work, I hope you will have a nice daytime!
4/9/2010 12:21:15 PM #
Great post, you’ve helped me a lot
4/28/2010 5:37:16 AM #
if i upgrade my blog, is it going to keep my same settings?
5/14/2010 2:42:10 PM #
nice post which really help us.well done best luck.
5/22/2010 6:50:20 AM #
Yo, just a shoutout, nice work
Loni Gittings
Loni Gittings
5/30/2010 1:22:10 AM #
Hi there, I found your blog via Google while searching for muscle building and your post looks very interesting for me.
6/3/2010 6:21:18 PM #
I enjoyed that, breath of fresh air.
6/3/2010 6:26:08 PM #
Nice one dude, I really like your work..
6/3/2010 11:33:45 PM #
Thanks for taking the time to sharing the updates...extremely useful for me.
6/4/2010 12:48:07 PM #
This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. Thanks for this wonderful resource!
6/5/2010 4:18:10 AM #
Just thought you might like to know I came to your website from the first page of aBing seach. Hard Work. I know how tidious it is to get your your site on the first page of a search. Ive downloaded ebook after ebook and finally got <A href="downloadable-mp3-music.org/">Downloadable mp3 music</A> on the first page. thank gosh only took 3weeks!!
6/6/2010 6:17:00 AM #
Just thought you might like to know I found this page from the first page of aGoogle seach. Hard Work. I know how nerve raking it is to get your your site on the first page of a search. Ive purchased ebook after ebook and now got <A href="http://nurfguns.net/">Nerf Toys</A></SPAN></SPAN></SPAN> on the first page. whew!! only took 3weeks!!
6/7/2010 7:41:30 PM #
Thought you may like to know I found this webpage from the first page of aYahoo seach. Great Job. I know how difficult it is to get your this page on the first page of a search. Ive purchased ebook after ebook and now got <A href="http://nerfgunsforsale.org/">Nerf Guns</A></SPAN></SPAN></SPAN> on the first page. sheesh only took 6weeks!!
6/8/2010 6:17:57 AM #
Thought you may like to know I clicked your site from the first page of aGoogle seach. Nice work. I know how difficult it is to get your this page on the first page of a search. Ive bought program after program and now got <A href="http://downloadable-mp3-music.org/">Music downlaods</A> on the first page. whew!! only took 6months!!
6/8/2010 6:30:24 PM #
Wonderful blog! I saw it at Bing and I must say that entries are well thought of. I will be coming back to see more posts soon.
6/12/2010 3:59:15 AM #
The World Cup will be very intense!  It comes down to flip of a coin with my opinion but best of luck to both teams! I'll be watching the entire match!
6/12/2010 8:03:04 PM #
lol, I really think I get this. 100% with you here. Some might disagree, but don't listen to them!
6/14/2010 12:09:30 AM #
This Cup should be extremely intense!   I give it a 50/50 chance in my opinion but best of luck to both teams! I'll be watching the entire match!
6/14/2010 2:02:30 PM #
hmmm...I think I know what you want to say, but could you explain me more about this? I'll appreciate it.
6/18/2010 10:18:27 AM #
I was rather glad to find this website. I wanted to appreciate this outstanding read!! I most definitely relished every little bit  and I have you bookmarked to check out fresh new stuff you publish.
6/18/2010 9:07:00 PM #
What I wouldnt give to learn how you got your design to be so amazing!  I mean it.  Besides the blog just being awesome, this page is too sweet!  Its not too flashy.  It doesnt do too much with colours and things and the videos you use are perfect for this topic!  Really, awesome blog.
6/25/2010 10:49:54 AM #
I've really enjoyed digesting your site. Thanks!
7/26/2010 6:48:26 AM #
Somebody recommended my this site when I was looking for some niche related articles.
7/26/2010 7:19:17 AM #
Somebody recommended my this site when I was looking for some niche related articles.
7/26/2010 7:47:40 AM #
Somebody recommended my this site when I was looking for some niche related articles.
7/26/2010 8:15:40 AM #
Somebody recommended my this site when I was looking for some niche related articles.
7/26/2010 9:07:45 AM #
Somebody recommended my this site when I was looking for some niche related articles.
7/26/2010 9:34:25 AM #
Somebody recommended my this site when I was looking for some niche related articles.
7/26/2010 10:01:31 AM #
Somebody recommended my this site when I was looking for some niche related articles.
8/5/2010 9:06:50 PM #
I disagree with most folks here; I started reading this blog post I couldn't stop until , while it wasn't just what I had been trying to find, was a great read though. I will instantaneously get your RSS feed to stay in touch of any updates.
8/7/2010 11:06:04 PM #
looks great I was planning to subscribe to your blog
8/14/2010 4:57:19 PM #
I differ with most people here; I found this blog post I couldn't stop until I was done, while it wasn't just what I had been looking for, was still a fantastic read though. I will instantly grab your RSS feed to keep informed of future updates.
8/15/2010 11:06:24 PM #
I disagree with most guys here; since I found this post I couldn't stop until I was done, although it wasn't just what I had been trying to find, was still a fantastic read though. I will immediately grab your RSS feed to stay in touch of future updates.
8/16/2010 5:53:05 AM #
Totally new to me; I didn’t know the many ramifications and depth to the story until I searched here through Google! Great job.
8/16/2010 6:00:58 AM #
That's news to me; I wasn't aware of the many ramifications and depth to the story until I surfed here through Google! Fantastic job.
8/16/2010 8:06:13 AM #
Thank you for making a sincere effort to explain this. I feel very strong about this and would like to nkwo more. If you can, as you learn more in depth knowledge, would you mind writing more posts similar to this one with more tips?
8/16/2010 8:16:10 AM #
Thanks for making a truthful effort to explain this. I feel very strong about it and would like to read more. If you can, as you learn more in depth knowledge, would you mind writing more posts similar to this one with more tips?
8/16/2010 9:48:05 PM #
I differ with most people here; since I started reading this blog post I couldn't stop until I finished,  even though it wasn't just what I had been searching for, was a fantastic read though. I will instantaneously take your feed to maintain in touch of any updates.
8/18/2010 3:45:00 PM #
Actually, I’m just beginning in management media and starting to find out how to do it well - resources like this blog post are very helpful. As our company is based in the US, is kind of new to us The case above is something that I worry about as well, how to show your own authentic enthusiasm and contribute to the fact.
8/18/2010 3:56:54 PM #
In fact, I’m just beginning in marketing media and working on to learn how to do it well - resources like this blog are a great resource. As our website is based in the US, is kind of new to us The example mention is something that I worry too well, how to show your own authentic enthusiasm and share to the opportunity.
8/27/2010 1:16:07 PM #
She is just great! All she touches converts to gold, so regardless of some gossips or lies about her, she keeps on the top. Way to go girl, we love you
8/30/2010 8:48:34 PM #
In point of fact, I’m just starting out in marketing media and trying to find out how to do it well - resources like this blog are very helpful. As our Site is based in the US, is kind of new to us The case above is something that I worry about as well, how to show your own genuine enthusiasm and share to the fact.
9/1/2010 11:25:07 AM #
Actually, I’m just getting my feet wet in community management/marketing media and starting to learn how to do it well - resources like this article are a great resource. As our website is based in the US, is kind of new to us The reference mention is something that I worry about as well, how to show your own real enthusiasm and contribute to the community.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading