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

In my previous post, https://realmpksharepoint.wordpress.com/2014/04/01/get-all-the-webparts-of-a-sharepoint-webpage-using-client-object-model-c/ , 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=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");

sbEnvelope.Append(String.Format(
"<soap:Body>" +
    "<GetWebPart2 xmlns=\"http://microsoft.com/sharepoint/webpartpages\">" +
        "<pageurl>{0}</pageurl>" +
        "<storageKey>{1}</storageKey>" +
        "<storage>{2}</storage>" +
        "<behavior>Version3</behavior>" +
    "</GetWebPart2>" +
"</soap:Body>"
, pageUrl, storageKey, "Shared"));
sbEnvelope.Append("</soap:Envelope>");

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(webServiceUrl);
req.Method = "POST";
req.ContentType = "text/xml; charset=\"utf-8\"";
req.Accept = "text/xml";
req.Headers.Add("SOAPAction", "\"http://microsoft.com/sharepoint/webpartpages/GetWebPart2\"");
req.UseDefaultCredentials = false;

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

string authCookieValue = spCredentials.GetAuthenticationCookie(targetSite);
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(
    new Cookie("FedAuth",
        authCookieValue.TrimStart("SPOIDCRL=".ToCharArray()),
        String.Empty,
        targetSite.Authority));

using (Stream stream = req.GetRequestStream())
{
    using (StreamWriter writer = new StreamWriter(stream))
    {
        writer.Write(sbEnvelope.ToString());
    }
}

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

XmlDocument xDoc = new XmlDocument();
xDoc.Load(responseStream);

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).

    Like

  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.

    Like

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:

WordPress.com Logo

You are commenting using your WordPress.com 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.