Get WebPart Page of a SharePoint site using Web Services C#

In my previous post, , I had explained, how we can use the WebService, WebPartPages.asmx. Here, I am going to use another method of this WebService, GetWebPartPage. This method, returns the content of the html page. The contents are as such that, they don’t include .js/.css files references. The returned string, contains, Page title, properties[MetaInfo], and all the WebParts [in a proper order] as they are present on the page.Now, all the WebParts, always have ZoneId of the page, in which they have been added. ZoneId, is a very important property, required to add a WebPart to a Zone. However, for some inexplicable reasons, neither the GetWebPart2 method of this service returns ZoneId nor does the SharePoint CSOM. They do for some WebParts, but for most they don’t return info regarding ZoneID.

It is for this reason, I was forced to call this method to get the info of all the WebParts at one go. Then, I had to parse this, returned string value, to identify the ZoneId of each of the WebParts based on their Guids. To get the Guids of each of the WebParts, you can use the SharePoint CSOM demonstrated here, .

Following is the implementation of the method, GetWebPartPage.

string webPageInfo = String.Empty; 
string webServiceUrl = ctx.Web.Url + "/_vti_bin/WebPartPages.asmx";

//say, we're trying to get the Home.aspx item of the List, SitePages. 
string documentName = String.Concat("SitePages/", listItem.FieldValuesAsText.FieldValues["FileLeafRef"]);

StringBuilder sbEnvelope = new StringBuilder();
sbEnvelope.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sbEnvelope.Append("<soap:Envelope xmlns:xsi=\"\" xmlns:xsd=\"\" xmlns:soap=\"\">");
    "<soap:Body>" +
        "<GetWebPartPage xmlns=\"\">" +
            "<documentName>{0}</documentName>" +
        "</GetWebPartPage>" +
    , WebUtility.HtmlEncode(documentName)));

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(webServiceUrl);
req.Method = "POST";
req.ContentType = "text/xml; charset=\"utf-8\"";
req.Accept = "text/xml";
req.Headers.Add("SOAPAction", "\"\"");
req.UserAgent = "FrontPage";
req.UseDefaultCredentials = false;

Uri targetSite = new Uri(ctx.Web.Url);
SharePointOnlineCredentials spCredentials = (SharePointOnlineCredentials)ctx.Credentials;

string authCookieValue = spCredentials.GetAuthenticationCookie(targetSite);
req.CookieContainer = new CookieContainer();
    new Cookie("FedAuth",

using (Stream stream = req.GetRequestStream())
    using (StreamWriter writer = new StreamWriter(stream))

WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();

XmlDocument xDoc = new XmlDocument();

if (xDoc.DocumentElement != null && xDoc.DocumentElement.InnerText.Length > 0)
    webPageInfo = xDoc.DocumentElement.InnerText;

    //webPageInfo = webPageInfo.Substring(webPageInfo.IndexOf(""));

    //The above commented subString code was used further 
    //to implement the logic of parsing. Since we're not 
    //concerned with that hence it's not included here.


Recently I started getting, 403 Forbidden error while running this code. Certain modifications are required, to make this code work. They are defined in the following link,

403 Forbidden Error while calling GetWebPartPage SharePoint Online


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s