0

I am using the below code. While debugging I always get the error :

"Collection not initialised"

in line 3

private static void GenerateLeftNav(ClientContext cltContext, string webUrl)
        {
            cltContext.Load(cltContext.Web);
            cltContext.ExecuteQuery();
            NavigationNodeCollection qlNavNodeColl = cltContext.Web.Navigation.QuickLaunch;
            cltContext.Load(qlNavNodeColl);
            //Fetching node which needs to be updated

            //   IEnumerable<NavigationNode> projectsNode = cltContext.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Projects"));
            NavigationNode projectsNode = qlNavNodeColl.Where(n => n.Title == "Projects").FirstOrDefault();
            if (projectsNode != null)
            {
                try
                {
                    NavigationNode gNode = projectsNode;
                    gNode.Url = webUrl;
                    gNode.Update();
                    cltContext.ExecuteQuery();
                }
                catch (Exception)
                {
                    throw;
                }
            }

    }

2 Answers 2

1

You are missing the executeQuery after loading the quick launch navigation node collection.

Modify the code as below:

cltContext.Load(cltContext.Web);
cltContext.ExecuteQuery();
NavigationNodeCollection qlNavNodeColl = cltContext.Web.Navigation.QuickLaunch;
cltContext.Load(qlNavNodeColl);
//add the execute query method
cltContext.ExecuteQuery();
1
  • welcome , glad i could help :) , can you mark it as answer, so that it wont show up in the unanswered section ? Its just a checkmark below the upvote/downvote buttons. Oct 6, 2017 at 9:34
4

I tried your code at my end. Everything is correct except one line.

Remove cltContext.ExecuteQuery(); from second line and just keep cltContext.ExecuteQuery(); after cltContext.Load(qlNavNodeColl);.

So your final code will be like

private static void GenerateLeftNav(ClientContext cltContext, string webUrl)
    {
        cltContext.Load(cltContext.Web);
        NavigationNodeCollection qlNavNodeColl =ltContext.Web.Navigation.QuickLaunch;
        cltContext.Load(qlNavNodeColl);
        cltContext.ExecuteQuery();
        NavigationNode projectsNode = qlNavNodeColl.Where(n => n.Title == "Projects").FirstOrDefault();
        if (projectsNode != null)
        {
             try
             {
                 NavigationNode gNode = projectsNode;
                 gNode.Url = webUrl;
                 gNode.Update();
                 cltContext.ExecuteQuery();
              }
              catch (Exception)
              {
                 throw;
              }
       }
    }

Your Answer

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

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