Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, December 03, 2012

Getting SubWeb URLS and Titles using ECMA Script Client Object Model

var context = null;
var web = null; 
function ShowAllSubsites()
{
    try
    {
         context = new SP.ClientContext.get_current();
         web = context.get_web().get_webs();
         context.load(web);
         context.executeQueryAsync(Function.createDelegate(this, this.SubWebsSuccessMethod), Function.createDelegate(this, this.SubWebsFailedMethod));    
    }
    catch(Ex)
    {
        alert(Ex);
    }
}

function SubWebsSuccessMethod(sender, args)
{
    var strSubwebs="";
    strSubwebs+="<Table>";
    strSubwebs+="<Tr>";
    strSubwebs+="<Td class='HeadingClass'>";
    strSubwebs+="SubWeb Name";
    strSubwebs+="</Td>";
    strSubwebs+="</Tr>";
    for(var i=0;i<this.web.get_count();i++)
    {
        var strSubWeb=this.web.itemAt(i);
        var strSubWebUrl=strSubWeb.get_serverRelativeUrl();
        var strSubwebTitle=strSubWeb.get_title();
        strSubwebs+="<Tr>";
        strSubwebs+="<Td class='LoopRecordsClass'>";
        strSubwebs+="<a href='"+strSubWebUrl+"'>"+strSubwebTitle+"</a>";
        strSubwebs+="</Td>";
        strSubwebs+="</Tr>";
    }
    strSubwebs+="</Table>";
    $("#divSubWebs").html(strSubwebs);
}

ExecuteOrDelayUntilScriptLoaded(ShowAllSubsites, "sp.js");

function SubWebsFailedMethod(sender, args)
{
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}

No comments:

Post a Comment