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 ..
}
}