Import Publishing Page Content from XML

 

It seems like a good move to compliment the recent Content Export article with one about importing XML content into your MOSS Publishing Site (which might be a variation). The good news is that this process is fairly straightforward. (If you’ve output sufficient identifiers when you exported your content that is!)

 

Below is some sample export XML;

 <pages>

- <web guid="4a167efa-837b-40d6-a39b-f17b70f6ce61">

- <page url="https://mydomain.com/FR/Pages/GlossaryofTerms.aspx" guid="0e699d23-248f-416f-84e6-42820c8d4024">

- <fields> 

<field guid="fb564e0f-0b70-4ab9-b863-0177e6ddd247" fieldname="Title">Glossary of Terms</field>  

</fields> 

</page> 

</web> 

</pages> 

We have a collection of Webs, those webs contain pages, and those pages contain fields. Each level of that hierarchy web->Page->Field has its appropriate Guid so we can find it again easily. The url attribute of page is for our human readership, as is the fieldname attribute for field. (btw “Glossary of Terms” is just the basic content for this particular field).

 

So, we are going to iterate through the XML, obtaining references to web, page, and field instances as appropriate before pushing page content into them. Here we go;

 

Get our Site Collection

using (SPSite site = new SPSite(SPContext.Current.Site.Url))

 

Get the web guid from the XML and use that to get our publishing web;

string webGuid = webNode.Attributes[xmlGuid].Value; 

PublishingWeb pw = PublishingWeb.GetPublishingWeb(site.OpenWeb(new Guid(webGuid)));

 

Now we have our publishing web we get the Publishing Pages very easily;

PublishingPageCollection ppCol = pw.GetPublishingPages();

 

We know from the XML that the publishing pages we’re interested in reside in that web so we get the appropriate identifiers, and access them;

 string pageGuid = pageNode.Attributes[xmlGuid].Value; 

PublishingPage pp = ppCol[new Guid(pageGuid)];

 

Depending on your setup, you may also need to do a checkout at this point as we will be modifying the page;

 

pp.CheckOut();

  

Finally, set the content, and update the page, and the job is done;

 string fieldGuid = fieldNode.Attributes[xmlGuid].Value;

string fieldContent = SPHttpUtility.HtmlDecode(fieldNode.InnerText); //Set the content

pp.ListItem[new Guid(fieldGuid)] = fieldContent; 

pp.Update();//Update after completing all field changes, not after each one 

You might need a pp.Checkin() at this stage, but that depends on your setup.

 

Iterate through the XML in that way, and the import is done! This code avoids the iteration of the export code, but is more processor intensive because we are changing items rather than just reading them.

 

So, you now have a simple solution to start translating your variations content.