Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7084589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:19:14+00:00 2026-05-28T07:19:14+00:00

Not sure where to begin. I have code setup to create a new site

  • 0

Not sure where to begin. I have code setup to create a new site and then copy the look, lists, and webparts from a template site.

IT WORKS FINE WHEN LOGGED IN AS SYSTEM ADMIN!

foreach (Microsoft.SharePoint.WebPartPages.WebPart webPartTemplate in webPartCollectionTemplate)
{
  try
  {
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
webPartManagerTemplate.ExportWebPart(webPartTemplate, xmlWriter);
xmlWriter.Flush();
memoryStream.Flush();
memoryStream.Position = 0;

XmlTextReader xmlReader = new XmlTextReader(memoryStream);

Microsoft.SharePoint.WebPartPages.ListViewWebPart listViewWebPartTemplate = null;
Microsoft.SharePoint.WebPartPages.ListViewWebPart listViewWebPartTarget = null;
Microsoft.SharePoint.WebPartPages.WebPart webPartTarget = null;

bool webPartIsListViewWebPart = false;

// If it throws an exception, the web part is not a ListViewWebPart
try
{
  listViewWebPartTemplate = (ListViewWebPart)webPartTemplate;
  webPartIsListViewWebPart = true;
}
catch { }

if (webPartIsListViewWebPart)
{
  string errorMessage = "";
  listViewWebPartTarget = (Microsoft.SharePoint.WebPartPages.ListViewWebPart)webPartManagerTarget.ImportWebPart(xmlReader, out errorMessage);

  if (errorMessage != null)
    // log it

  // Drop the brackets from the Guid and create a Guid object from the string. Then use it to find the list so we can get the title.
  SPList listTemplate = webTemplate.Lists[new Guid(listViewWebPartTemplate.ListName.ToString().TrimStart('{').TrimEnd('}'))];
  SPList listTarget = webTarget.Lists[listTemplate.Title];

  listViewWebPartTarget.ListName = listTarget.ID.ToString("B").ToUpper();
  listViewWebPartTarget.WebId = webTarget.ID;
  listViewWebPartTarget.TitleUrl = webPartTemplate.TitleUrl.ToString().Replace(siteTemplate.ServerRelativeUrl, siteTarget.ServerRelativeUrl);
  listViewWebPartTarget.Title = webPartTemplate.Title;
  listViewWebPartTarget.ZoneID = webPartTemplate.ZoneID;

  webPartDictTarget.Add(listViewWebPartTarget.ZoneID + webPartTemplate.ZoneIndex.ToString(), listViewWebPartTarget);
}
else
{
  string errorMessage = "";
  webPartTarget = (Microsoft.SharePoint.WebPartPages.WebPart)webPartManagerTarget.ImportWebPart(xmlReader, out errorMessage);

  if (errorMessage != null)
    // log it

  webPartTarget.ChromeType = webPartTemplate.ChromeType;
  webPartTarget.TitleUrl = webPartTemplate.TitleUrl.ToString().Replace(siteTemplate.ServerRelativeUrl, siteTarget.ServerRelativeUrl);
  webPartTarget.Title = webPartTemplate.Title;
  webPartTarget.ZoneID = webPartTemplate.ZoneID;

  webPartDictTarget.Add(webPartTarget.ZoneID + webPartTemplate.ZoneIndex.ToString(), webPartTarget);
}
  }
  catch (Exception ex)
  {
  }
}

// Acquire keys and sort them.
List<string> list = new List<string>(webPartDictTarget.Keys);
list.Sort();

// Loop through keys.
int i = 0;
foreach (var key in list)
{
  try
  {
    Microsoft.SharePoint.WebPartPages.WebPart wpTmp = (Microsoft.SharePoint.WebPartPages.WebPart)webPartDictTarget[key];
    webPartManagerTarget.AddWebPart(wpTmp, wpTmp.ZoneID, i);
  }
  catch (Exception ex)
  {
  }
  i++;
}

The error is thrown on the following line:

webPartManagerTarget.AddWebPart(wpTmp, wpTmp.ZoneID, i);

The stack trace follows {“There was an error generating the XML document.”}:

at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle, String id) at
System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,
Object o) at
Microsoft.SharePoint.WebPartPages.WebPart.SerializeXml(XmlWriter
writer, XmlSerializer xmls, Boolean shouldSerializeAll) at
Microsoft.SharePoint.WebPartPages.WebPart.WriteXmlAsBytes(XmlSerializer
xmls) at
Microsoft.SharePoint.WebPartPages.WebPart.WriteXmlGlobal(Boolean
disableSafeControlsCheck) at
Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(PersonalizationScope
scope) at
Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.get_Links()
at
Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartToStore(WebPart
webPart, Int32 viewId, String viewGuid) at
Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart
superset, Boolean throwIfLocked) at
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(WebPart
webPart, String zoneId, Int32 zoneIndex, Boolean throwIfLocked) at
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(WebPart
webPart, String zoneId, Int32 zoneIndex) at
admSitePanelSolution.admSiteCreateCollection.<>c_DisplayClasse.b_c()

And the InnerException {“Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))”}:

at
Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException
ex) at
Microsoft.SharePoint.Library.SPRequest.GetViewsSchemaXml(String
bstrUrl, String bstrListName, Boolean bFullBlown, ISP2DSafeArrayWriter
p2DWriter, Int32& plDefaultViewIndex) at
Microsoft.SharePoint.SPViewCollection.EnsureViewSchema(Boolean
fullBlownSchema) at
Microsoft.SharePoint.SPViewCollection..ctor(SPList list) at
Microsoft.SharePoint.SPList.get_Views() at
Microsoft.SharePoint.SPList.get_DefaultView() at
Microsoft.SharePoint.SPList.get_DefaultViewUrl() at
Microsoft.SharePoint.WebPartPages.ListViewWebPart.ShouldSerializeDetailLink()
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterListViewWebPart.Write9_ListViewWebPart(String
n, String ns, ListViewWebPart o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterListViewWebPart.Write10_WebPart(Object
o)

Can anyone provide some help of why this happens when not logged in as system admin. This whole block also runs inside elevated privileges and the SPSite and SPWeb were also instantiated inside the elevated block. This code is an excerpt.

UPDATE

If I make the user a site collection owner of the base site collection, then the code above runs fine. So what in the code above resets context or needs to reference the base site collection. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:19:15+00:00Added an answer on May 28, 2026 at 7:19 am

    And my colleague swoops in to save the day! In SP dev, there is a well known ‘hack’ which is used a lot when permissions get weird and stupid. It is to nullify the HttpContext. I tried this around the line and loop which was giving me an issue but it appears that you have to place it much higher in the code during the import.

    Here is the new fix.

    HttpContext currentContext = HttpContext.Current;
    HttpContext.Current = null;
    listViewWebPartTarget = (Microsoft.SharePoint.WebPartPages.ListViewWebPart)webPartManagerTarget.ImportWebPart(xmlReader, out errorMessage);
    HttpContext.Current = currentContext;
    

    This code now works for copying webparts from one apsx page to another. Hope it helps someone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not even sure how to begin wording this question, but basically, I have
To begin with, I'm not even sure, if it is the right way to
Not sure what's going on here. I have a DateTime object, and when I
I'm not sure exactly what the following class does that we have for a
I have a trigger that's throwing an error, and I am not sure how
I have been dealing with FTP lately and I'm not sure about the security
I think its quite a simple question but not sure. I have a class:
I'm not sure how this piece of code works. [Serializable] class Blah { public
I just begin my JPA 2.0 studies, and I have this piece of code:
This is a weird bug, and I'm not even sure how to begin figuring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.