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.
where is the storageKey coming from when building the envelope?
It's not declared…
LikeLike
storageKey is the WebPartDefinition’s ID i.e., the ID of the given web part whose schema we are trying to retrieve here. To get the id, have a look at the following url,
https://realmpksharepoint.wordpress.com/2014/04/01/get-all-the-webparts-of-a-sharepoint-webpage-using-client-object-model-c/
LikeLike
[…] my previous post, https://realmpksharepoint.wordpress.com/2014/04/03/get-the-schemaxml-of-a-particular-webpart-of-a-sh… , I had explained, how we can use the WebService, WebPartPages.asmx. Here, I am going to use […]
LikeLike
Can we use it for On-prem SharePoint? I am getting 401 unauthorized (I’m passing req.Headers.Add(“X-RequestDigest”, digest).
LikeLike
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.
LikeLike