0

I need to find all the subsites of particular sites and fill the drop down using ClientContext Object

2
    function RetriveSites()
                    {
                    currentcontext = new SP.ClientContext(‘/collaboration/KeyWordsDemo’);   
    /* to get the current context use this instead -à.get_current();*/ 
     /*more info http://msdn.microsoft.com/en-us/library/ee658863.aspx*/
                    currentweb = currentcontext.get_web();
                    this.subsites = currentweb.get_webs();
                    currentcontext.load(this.subsites);
                    currentcontext.executeQueryAsync(
                                    Function.createDelegate(this, this.ExecuteOnSuccess),
                                    Function.createDelegate(this, this.ExecuteOnFailure));
                    }

ExecuteOrDelayUntilScriptLoaded(RetriveSites,’sp.js’);

}

Html of Dropdown which we are going to bind with sites

<select id=”teamsiteSelect” onchange=”navNow();”>
                <option value=”#”>Select Team Site…</option>
                <option value=”/collaboration/KeyWordsDemo”>Home</option>
</select>

code to bind dropdown

function ExecuteOnSuccess(sender, args) {
                var subs =”;
                var Siteenum = this.subsites.getEnumerator();
                while (Siteenum.moveNext())
                                {
                                var Site = Siteenum.get_current();
                                $(‘#teamsiteSelect’).append( $(‘<option></option>’).val(Site.get_serverRelativeUrl).html(Site.get_title));
                                }

                }

function ExecuteOnFailure(sender, args) {
                //alert(“error”);
}

for more detail follow:

http://slalomdotcom.wordpress.com/2012/08/03/creating-a-drop-down-navigation-menu-in-sharepoint-using-javascript/

  • mark it as answer if it resolved your issue. – Anuja Nov 27 '14 at 5:45
1

Option 1

Use Web.Webs property to get a web sites immediately beneath the Web site:

using (var ctx = new ClientContext(webUri))
{
    var subWebs = ctx.Web.Webs;
    ctx.Load(subWebs);
    ctx.ExecuteQuery();
}

Option 2

The following example demonstrates how to retrieve all web sites:

public static void LoadAllWebs(Web web,List<Web> allWebs)
{
    var ctx = web.Context;
    ctx.Load(web);
    ctx.Load(web.Webs);
    ctx.ExecuteQuery();
    allWebs.Add(web);
    foreach (var subWeb in web.Webs)
    {
       LoadAllWebs(subWeb, allWebs);
    }
}

Usage:

using (var ctx = new ClientContext(webUri))
{
    //load all webs in site collection
    var allWebs = new List<Web>();
    LoadAllWebs(ctx.Site.RootWeb,allWebs);

    //print 
    foreach (var web in allWebs)
    {
       Console.WriteLine(web.Title);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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