Scenario:
While using RunWithElevatedPrivileges you got following error.
Error : Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack when use new spsite
Solution:
SPSite and SPWeb object should be created inside delegate(), example below. If not you might get the above error.
Wrong Code:
string siteUrl = @"http://localhost";Correct Code:
SPSite site = null ;
SPWeb web = null ;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
site = new SPSite(siteUrl);
web = SPSDestSite.OpenWeb();
// To do ( run code here )
});
web.Close();
site.Close();
string siteUrl = @"http://localhost";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = SPSDestSite.OpenWeb())
{
// To do ( run code here )
}
}
});
5 comments:
Thanks for sharing.
it helps me solving this error.
Hi Sandeep,
I have a similar problem. We are using sharepoint "GetUsageData" method to get MySite URL details. I followed the same steps you posted, but still I am getting the problem.
Hi,
We are using sharepoint "GetUsageData" method to get all the details of MySite URL. how do we data from this ?
Hi,
we are using a sharepont method "GetUsageData" for MySite url access and get the details. I am facing the same problem while getting the data with "GetUsageData" method along with RunWithElevatedPrivileges
Hi,
I too have a similar problem.
But I got solved with 1 day of research work.
Here the problem is authorization. We need to provide authorization to GetUsageData method. How we can achieve this is by using SPUserToken object.
Here is the code.
SPUserToken token = personalSite.SystemAccount.UserToken;
(here personalSite = "http://...")
now
string personalUrl = UserProfile.PropertyContstant.PersonalUrl.ToString();
using(SPSite site = new SPSite(personalUrl,token)
{
using(SPWeb web = site.OpenWeb())
{
DataTable Dt = web.GetUsageData (.....);
{
}
}
Note that all the above code should be placed under RunWithElevatedPrivileges method.
Post a Comment