HAcalahan
asked on
UnauthorizedAccess Exception: When copying Sharepoint 2010 list item
I am looking for assistance to resolve an exception being thrown when I attempt to copy an
added Blog list item from one Sharepoint WebApplication/ContenctDB( Webapp0) to a Blog list on a different Sharepoint WebApplication(Webapp1). The exception raised is
System.UnauthorizedAccessE xception, however I am the primary adminstrator for both sites,
plus I am executing the code using RunWithElevatedPrivileges. Help decyphering the error
would be appreicated.
I have attached the code.
The Exception being thrown is:
Error loading and running event receiver CopyBlogListItem.CopyBlogL istItem in CopyBlogListItem,
Version = 1.0.0.0, Culture=neutral, PublicKeyToken=ac27c065e1c 5c56b.
Additional information is below.
: CopyBlogListItem, SPSecurity.RunWithElevated Privileges (copyListI tem):
Exception: [System.Exception: CopyBlogListItem, targetItem.Update():
Exception: [System.UnauthorizedAccess Exception:
<nativehr>0x80070005</nati vehr>
<nativestack></nativestack >
at Microsoft.SharePoint.Libra ry.SPReque st.AddOrUp dateItem
(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate,
Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID,
String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion,
Object& pvarAttachmentNames, Object& pvarAttachmentContents,
Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration,
Boolean bPublish, String bstrFileName,
ISP2DSafeArrayWriter pListDataValidationCallbac k,
ISP2DSafeArrayWriter pRestrictInsertCallback,
ISP2DSafeArrayWriter pUniqueFieldCallback)
at Microsoft.SharePoint.SPLis tItem.AddO rUpdateIte m
(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion,
Boolean bNoVersion, Boolean bMigration, Boolean bPublish,
Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID,
Object& objAttachmentNames, Object& objAttachmentContents,
Boolean suppressAfterEvents, String filename)
at Microsoft.SharePoint.SPLis tItem.Upda teInternal (Boolean bSystem,
Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration,
Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin,
Boolean suppressAfterEvents, String filename)
at Microsoft.SharePoint.SPLis tItem.Upda te()
at CopyBlogListItem.CopyBlogL istItem.co pyListItem ()].
at CopyBlogListItem.CopyBlogL istItem.co pyListItem ()
at Microsoft.SharePoint.SPSec urity.<>c_ _DisplayCl ass4.<RunW ithElevate dPrivilege s>b__2()
at Microsoft.SharePoint.Utili ties.Secur ityContext .RunAsProc ess
(CodeToRunElevated secureCode)
at Microsoft.SharePoint.SPSec urity.RunW ithElevate dPrivilege s
(WaitCallback secureCode, Object param)
at Microsoft.SharePoint.SPSec urity.RunW ithElevate dPrivilege s
(CodeToRunElevated secureCode)
at CopyBlogListItem.CopyBlogL istItem.It emAdded(SP ItemEventP roperties properties)].
added Blog list item from one Sharepoint WebApplication/ContenctDB(
System.UnauthorizedAccessE
plus I am executing the code using RunWithElevatedPrivileges.
would be appreicated.
I have attached the code.
The Exception being thrown is:
Error loading and running event receiver CopyBlogListItem.CopyBlogL
Version = 1.0.0.0, Culture=neutral, PublicKeyToken=ac27c065e1c
Additional information is below.
: CopyBlogListItem, SPSecurity.RunWithElevated
Exception: [System.Exception: CopyBlogListItem, targetItem.Update():
Exception: [System.UnauthorizedAccess
<nativehr>0x80070005</nati
<nativestack></nativestack
at Microsoft.SharePoint.Libra
(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate,
Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID,
String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion,
Object& pvarAttachmentNames, Object& pvarAttachmentContents,
Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration,
Boolean bPublish, String bstrFileName,
ISP2DSafeArrayWriter pListDataValidationCallbac
ISP2DSafeArrayWriter pRestrictInsertCallback,
ISP2DSafeArrayWriter pUniqueFieldCallback)
at Microsoft.SharePoint.SPLis
(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion,
Boolean bNoVersion, Boolean bMigration, Boolean bPublish,
Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID,
Object& objAttachmentNames, Object& objAttachmentContents,
Boolean suppressAfterEvents, String filename)
at Microsoft.SharePoint.SPLis
Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration,
Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin,
Boolean suppressAfterEvents, String filename)
at Microsoft.SharePoint.SPLis
at CopyBlogListItem.CopyBlogL
at CopyBlogListItem.CopyBlogL
at Microsoft.SharePoint.SPSec
at Microsoft.SharePoint.Utili
(CodeToRunElevated secureCode)
at Microsoft.SharePoint.SPSec
(WaitCallback secureCode, Object param)
at Microsoft.SharePoint.SPSec
(CodeToRunElevated secureCode)
at CopyBlogListItem.CopyBlogL
namespace CopyBlogListItem
{
public class CopyBlogListItem : SPItemEventReceiver
{
// Local variable to contain data passed in from sharepoint
private SPItemEventProperties _properties;
// Method called after a new item has been added
public override void ItemAdded(SPItemEventProperties properties)
{
// set up a log file
log = new HTMLFileLogging(c_logFileFolder, string.Format(LogFileName, properties.ListTitle, properties.OpenWeb().Title), true, false);
// run under system account
this.WriteToLog("ItemAdded: Executing Event Handler", true);
_properties = properties;
this.WriteToLog("ItemAdded: Properties Set", true);
try
{
SPSecurity.RunWithElevatedPrivileges(copyListItem);
}
catch (Exception e)
{
throw new Exception("CopyBlogListItem, SPSecurity.RunWithElevatedPrivileges(copyListItem): Exception: [" + e.ToString() + "].");
}
this.WriteToLog("ItemAdded: Event Handler Complete", true);
}
private void copyListItem()
{
//Step 0: get a handle to the item that raised the event
this.WriteToLog("copyListItem: Get handle to item", true);
SPListItem item = _properties.ListItem;
//step 1: Check category property to determine receiving site.
//***initialize targetURL***
this.WriteToLog("copyListItem: Initializing targetURL", true);
this.WriteToLog("copyListItem: Item Category is " + item["Category"] + ".", true);
string targetURL = "0";
string targetCategory = "";
if (item["Category"].ToString() == "1;#Energy Conservation")
{
this.WriteToLog("copyListItem: Match on Energy Conservation", true);
targetURL = "http://Webapp1:8083/blog/";
targetCategory = "1;#Energy Conservation";
}
this.WriteToLog("copyListItem: targetURL Set", true);
// Open the site if targetURL is set
if (targetURL != "0")
{
this.WriteToLog("copyListItem: Preparing to open targetSite", true);
using (SPSite targetSite = new SPSite(targetURL))
{
using (SPWeb targetWeb = targetSite.OpenWeb())
{
this.WriteToLog("copyListItem: targetSite Opened", true);
//Step 2: Set target list to receive new item
this.WriteToLog("copyListItem: Setting targetList", true);
SPList targetList = targetWeb.Lists["Posts"];
//Step 3: Initialize target list to receive copy of new item
this.WriteToLog("copyListItem: Initializing targetList", true);
SPListItem targetItem = targetList.Items.Add();
//Step 4: Initialize item properties on target list.
this.WriteToLog("copyListItem: Initializing targetItem", true);
targetItem["Location"] = item["Location"].ToString();
targetItem["Title"] = item["Title"].ToString();
targetItem["Body"] = item["Body"].ToString();
targetItem["Category"] = targetCategory.ToString();
targetItem["Published"] = System.DateTime.Now;
this.WriteToLog("copyListItem: Update targetItem", true);
try
{
targetItem.Update();
}
catch (Exception e)
{
this.WriteToLog("ERROR(targetItem.Update):" + e.Message.ToString(), true);
throw new Exception("CopyBlogListItem, targetItem.Update(): Exception: [" + e.ToString() + "].");
}
}
}
}
this.WriteToLog("copyListItem: Sub-Process Complete", true);
}
}
}
ASKER
Added C# zone
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Option 2: Impersonation worked great. Thank you.
ASKER