Get the SchemaXml of a particular WebPart of a SharePoint Page using Web Services C#

In my previous post, , I have explained how to get WebParts properties. Now, if you’re interested in getting the schemaXml of that WebPart (like exporting a WebPart from the SharePoint Page in edit mode) then you can’t do that using CSOM. Here, the only available solution is the WebService, WebPartPages.asmx. You can view all the functions of this WebService at the url, YourSiteUrl/_vti_bin/WebPartPages.asmx.

What I am going to use here, is the method, GetWebPart2. This method takes three parameters, complete page Url, WebPartGuid, behavior-Shared/User.

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

//GetCompleteUrl is a custom utility function to append the web url and page //serverRelativeUrl.
string pageUrl = Utility.GetCompleteUrl(ctx.Web.Url, pageServerRelativeUrl);

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>" +
    "<GetWebPart2 xmlns=\"\">" +
        "<pageurl>{0}</pageurl>" +
        "<storageKey>{1}</storageKey>" +
        "<storage>{2}</storage>" +
        "<behavior>Version3</behavior>" +
    "</GetWebPart2>" +
, pageUrl, storageKey, "Shared"));

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.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)
    webPartInfo = xDoc.DocumentElement.InnerText;

//webPartInfo is your WebParts SchemaXml.

5 thoughts on “Get the SchemaXml of a particular WebPart of a SharePoint Page using Web Services C#

  1. Can we use it for On-prem SharePoint? I am getting 401 unauthorized (I’m passing req.Headers.Add(“X-RequestDigest”, digest).


  2. Hi Piyush,

    I am getting below error,

    An unhandled exception of type ‘System.NullReferenceException’ occurred in CSOMWebSite.exe

    Additional information: Object reference not set to an instance of an object.

    line : SharePointOnlineCredentials spCredentials = (SharePointOnlineCredentials)ctx.Credentials;

    Please help me to resolve it.
    I am working in On Premise SP2013 Web site.


Leave a Reply to Get WebPart Page of a SharePoint site using Web Services C# | SharePoint Blog - Piyush Cancel reply

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

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.