3

Can we retrieve the TypeName of a web part using CSOM?

2
  • What would be the reason I get Access denied during the web service call? I have the highest permissions possible in the environment. If it makes a difference, I'm calling this from an auto-hosted SharePoint 2013 App.
    – user21884
    Commented Jan 10, 2014 at 14:43
  • I personally used server-side CSOM instead of Vadim's code so haven't checked the code. However, you could try to call other services. I think there should not be any specific permissions needed for this service in particular.
    – thomius
    Commented Jan 11, 2014 at 15:56

1 Answer 1

4

It doesn't seem possible to retrieve all the web part properties (like TypeName) via CSOM

But it could be performed via WebPartPagesWebService SharePoint Web Service as demonstrated below:

function getWebPartProperties(pageUrl,completeFn,errorFn)
{
    var soapEnv =
        '<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/">  \
            <soap:Body>  \
                <GetWebPartProperties2 xmlns="http://microsoft.com/sharepoint/webpartpages">  \
                    <pageUrl>' + pageUrl + '</pageUrl>   \
                    <storage>Shared</storage>   \
                    <behavior>Version3</behavior>   \
                </GetWebPartProperties2>  \
            </soap:Body>   \
        </soap:Envelope>';

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/WebPartPages.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: completeFn,
        error: errorFn,
        contentType: "text/xml; charset=\"utf-8\""
    });
}                


$(function() {
    getWebPartProperties('/SitePages/DevHome.aspx',
        function(xhr,textStatus){
            $(xhr.responseXML).find("WebPart").each(function() {
                var wpTypeName = $(this).find('type').attr('name'); //get Web Part TypeName
                console.log(wpTypeName);
            });

        },
        function(xhr,textStatus,errorThrown){
            console.log(errorThrown);
        });
});
3
  • Btw does this or something like this work for publishing pages?
    – thomius
    Commented Jan 4, 2014 at 13:59
  • I had a chance to verify it for publishing pages and it works for me Commented Jan 4, 2014 at 14:05
  • 1
    oh, apparently the error was not due to publishing pages but web part types. Tasks web part returns only one property "Cannot import this Web Part" apparently..
    – thomius
    Commented Jan 4, 2014 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.