Be careful when using properties and spcontext

Source: Internet
Author: User
Address: http://ayman-elhattab.blogspot.com/2008/09/be-careful-when-using-properties.html

What is spcontext?
In a word, the spcontext object can be used to obtain a site or web reference from the current context. It can be seen as a context for the HTTP request to SharePoint.

For example: Splist currentlist = Spcontext. Current. List;
Spweb currentsite
= Spcontext. Current. Web;
Spsite currentsitecollection
= Spcontext. Current. Site;
Spwebapplication currentwebapplication
= Spcontext. Current. Site. webapplication;
Splistitem item
= (Splistitem) spcontext. Current. item;
Spuser user
= Spcontext. Current. Web. currentuser;

 

What is "properties "?
The properties object contains information about a specific event and can be used to obtain references of the event-related spsite, spweb, splist, or splistitem.
For example:

Splistitem item = Properties. listitem;

However, be careful when using the properties parameter or spcontext. You cannot use properties to create your object. OtherwiseCodeError. The correct usage is to locate the entry through properties or spcontext, and then use the entry ID to create an object reference. Otherwise, your code will always have such problems.
You need to obtain all the guid IDs used before entering the permission escalation code block (runwithelevatedpriveleges) or simulating the user, and then create all the required objects in the security context, as shown below:

Guid siteid = Spcontext. Current. Site. ID;
Guid webid
= Spcontext. Current. Web. ID;
Guid listid
= Properties. listid;
Guid uniqueid
= Properties. listitem. uniqueid;
Spsecurity. runwithelevatedprivileges (
Delegate ()
{
Using (Spsite site =   New Spsite (siteid ))
{
Using (Spweb = Site. allwebs [webid])
{
Splist list
= Web. Lists [listid];
Splistitem listitem
= List. items [uniqueid];
// Code ..
}
}
});

Or use spusertoken to simulate the system account (this method is recommended)

Guid siteid = Spcontext. Current. Site. ID;
Guid webid
= Spcontext. Current. Web. ID;
Guid listid
= Properties. listid;
Guid uniqueid
= Properties. listitem. uniqueid;
Spusertoken systemaccounttoken
= Spcontext. Current. Site. systemaccount. usertoken;
Using (Spsite mysite =   New Spsite (siteid, systemaccounttoken ))
{
Using (Spweb myweb = Mysite. openweb (webid ))
{
Splist list
= Web. Lists [listid];
Splistitem listitem
= List. items [uniqueid];
// Code ..
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.