I have created a Visual Web Part. Inside, there is submit button that on click runs some code. It works fine with my Site Collection Administrator. but it doesn't work with my test user. This test user has read permissions only on the site.
When I click on the Visual Web Part button I get:
"Sorry this site is not shared with you”
are there any permission I need to set to enable a user to submit from a visual web part?
Edit: On click method:
protected void btninvia_Click(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite spsite = SPContext.Current.Site;
SPWeb spweb = spsite.RootWeb;
... code that works ....
SPListItem item = addMissione(user.User); // this method causes the authorization problem
... not executed code
});
}
addMission method:
private SPListItem addMissione(SPUser spuser)
{
SPListItem item =null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Logger.WriteLog(Logger.Category.Information, "addMissione", "spuser" + spuser.LoginName);
SPSite spsite = SPContext.Current.Site;
SPWeb spweb = spsite.RootWeb;
SPList splist = spweb.Lists["Missions"];
item = splist.AddItem();
item["Field1"] = spuser;
item["Field2"] = Field2.Text;
item["Start Date"] = startdate.SelectedDate;
....
item.Update();
Logger.WriteLog(Logger.Category.Information, "addMission", "item update"); // this code is not run
...
Logger.WriteLog(Logger.Category.Information, "addMissione", "fine addMissione");
});
return item;
}
usingaroundspsite.RootWebthough :) – Robert Lindgren May 18 '16 at 08:39usingas long as it's not explicitely bad... :) – Evariste May 18 '16 at 10:36SPWebthe same time as the parentSPSite. And, as you should not keep using objects from disposedSPSite/SPWebafter they're closed: here theusingstatement on theSPWebclearly shows theSPWebmust not be used any longer (and you won't be ale to reference the variable after theusingblock), while closing only the parentSPSitemay lead to a confusion about the status of theSPWeb. – Evariste May 18 '16 at 13:37