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.

4 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

Leave a 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 )

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