Fetching Node Collection and Children using CSOM in Sharepoint 2013
After a long Head banging, I found some solution for reading Node collection and its childs. :)
I'm not able to find any ready solution in Online. Hope it will be useful for you. Happy coding. :)
The following Code helps you for Fetching Node Collection and Children using CSOM in Sharepoint 2013.
using (ClientContext SourceSiteClientContext = new ClientContext(SourceSite))
{
//// Get the context for the SharePoint Site to access the data
using (ClientContext TargetSiteClientContext = new ClientContext(TargetSite))
{
Web SourceWeb = SourceSiteClientContext.Web;
Web TargetWeb = TargetSiteClientContext.Web;
List
NavigationNodeCollection SourceSiteTopNavigationColl, TargetSiteTopNavigationColl;
//Tries for Connecting Source Site with NTLM and if fails connects with Sharepoint online Method
try
{
Console.WriteLine("Connecting Source Site - " + SourceSite);
Console.WriteLine("Connecting with NTLM Method..");
SourceSiteClientContext.Credentials = new NetworkCredential(SourceSiteUserName, SourceSitePassword, SourceSiteDomain);
//// Get the collection of navigation nodes from the top navigation bar
SourceSiteTopNavigationColl = SourceWeb.Navigation.TopNavigationBar;
SourceSiteClientContext.Load(SourceSiteTopNavigationColl);
SourceSiteClientContext.ExecuteQuery();
}
catch
{
Console.WriteLine("Connecting with Sharepoint Online Method ..");
SecureString SourceSiteSecurePwd = convertToSecureString(SourceSitePassword);
SourceSiteClientContext.Credentials = new SharePointOnlineCredentials(SourceSiteUserName, SourceSiteSecurePwd);
//// Get the collection of navigation nodes from the top navigation bar
SourceSiteTopNavigationColl = SourceWeb.Navigation.TopNavigationBar;
SourceSiteClientContext.Load(SourceSiteTopNavigationColl);
SourceSiteClientContext.ExecuteQuery();
}
//Tries for Connecting Target Site with NTLM and if fails connects with Sharepoint online Method
try
{
Console.WriteLine("Connecting Target Site - " + TargetSite);
Console.WriteLine("Connecting with NTLM Method..");
TargetSiteClientContext.Credentials = new NetworkCredential(TargetSiteUserName, TargetSitePassword, TargetSiteDomain);
//// Get the collection of navigation nodes from the top navigation bar
TargetSiteTopNavigationColl = TargetWeb.Navigation.TopNavigationBar;
TargetSiteClientContext.Load(TargetSiteTopNavigationColl);
TargetSiteClientContext.ExecuteQuery();
}
catch
{
Console.WriteLine("Connecting with Sharepoint Online Method ..");
SecureString TargetSiteSecurePwd = convertToSecureString(TargetSitePassword);
TargetSiteClientContext.Credentials = new SharePointOnlineCredentials(TargetSiteUserName, TargetSiteSecurePwd);
//// Get the collection of navigation nodes from the top navigation bar
TargetSiteTopNavigationColl = TargetWeb.Navigation.TopNavigationBar;
TargetSiteClientContext.Load(TargetSiteTopNavigationColl);
TargetSiteClientContext.ExecuteQuery();
}
//Converts Relative Path to Absolute Path
Regex Splitter = new Regex("!|@|/");
String[] SourceParts = Splitter.Split(SourceSite);
string SourceSiteServerURL = SourceParts[0] + @"//" + SourceParts[2];
String[] TargetParts = Splitter.Split(TargetSite);
string TargetSiteServerURL = TargetParts[0] + @"//" + TargetParts[2];
//// Get the collection of navigation nodes from the Quick Launch Navigation bar
NavigationNodeCollection SourceSiteLeftNavigationColl = SourceWeb.Navigation.QuickLaunch;
SourceSiteClientContext.Load(SourceSiteLeftNavigationColl);
SourceSiteClientContext.ExecuteQuery();
//// Get the collection of navigation nodes from the Quick Launch Navigation bar
NavigationNodeCollection TargetSiteLeftNavigationColl = TargetWeb.Navigation.QuickLaunch;
TargetSiteClientContext.Load(TargetSiteLeftNavigationColl);
TargetSiteClientContext.ExecuteQuery();
bool isTopLinksEqual = SourceSiteTopNavigationColl.SequenceEqual(TargetSiteTopNavigationColl);
bool isQuickLaunchEqual = SourceSiteLeftNavigationColl.SequenceEqual(TargetSiteLeftNavigationColl);
//Checks Top Links are same
if (isTopLinksEqual != true)
{
//Removes Duplicates by Name
SourceSiteUniqueTopNavigationColl = SourceSiteTopNavigationColl.GroupBy(i => i.Title).Select(ss => ss.LastOrDefault()).ToList();
//Removes the Links from Target Site First
TargetSiteTopNavigationColl.ToList().ForEach(node => node.DeleteObject());
TargetSiteClientContext.ExecuteQuery();
Console.WriteLine("Reading Top Link Items ..");
//// Display all the node title which is available in the top navigation bar
foreach (NavigationNode TopNavigationNode in SourceSiteUniqueTopNavigationColl)
{
//// Get the collection of navigation nodes from the quick launch bar
NavigationNodeCollection TopLinksColl = TargetWeb.Navigation.TopNavigationBar;
try
{
// Describes a new navigation node to be created
NavigationNodeCreationInformation nodeCreation = new NavigationNodeCreationInformation();
//Tries for Adding with Target site Relative link, If error Catches and replaces with Server Link
nodeCreation.Title = TopNavigationNode.Title;
//Handles Relative URL
if (TopNavigationNode.Url.Trim() != "")
{
string NavLink = "";
if (TopNavigationNode.Url.StartsWith(RelativeStartLink))
{
NavLink = TargetSiteServerURL + TopNavigationNode.Url;
}
else
{
NavLink = TopNavigationNode.Url.Replace(SourceSiteServerURL, TargetSiteServerURL);
}
nodeCreation.Url = NavLink;
}
//// Add the new navigation node to the collection
nodeCreation.AsLastNode = true;
nodeCreation.IsExternal = TopNavigationNode.IsExternal;
TopLinksColl.Add(nodeCreation);
TargetSiteClientContext.Load(TopLinksColl);
TargetSiteClientContext.ExecuteQuery();
}
catch
{
// Describes a new navigation node to be created
NavigationNodeCreationInformation nodeCreation = new NavigationNodeCreationInformation();
//Tries for Adding with Target site Relative link, If error Catches and replaces with Server Link
nodeCreation.Title = TopNavigationNode.Title;
//Handles Relative URL
if (TopNavigationNode.Url.Trim() != "")
{
if (TopNavigationNode.Url.StartsWith(RelativeStartLink))
{
nodeCreation.Url = SourceSiteServerURL + TopNavigationNode.Url;
}
}
//// Add the new navigation node to the collection
nodeCreation.AsLastNode = true;
nodeCreation.IsExternal = TopNavigationNode.IsExternal;
TopLinksColl.Add(nodeCreation);
TargetSiteClientContext.Load(TopLinksColl);
TargetSiteClientContext.ExecuteQuery();
}
Console.WriteLine("Added Top Navigation Link - " + TopNavigationNode.Title);
CreateChildNode(TargetSiteServerURL, SourceSiteServerURL, SourceSiteClientContext, TargetSiteClientContext, TargetWeb, TargetWeb.Navigation.TopNavigationBar, TopLinksColl, TopNavigationNode);
}
}
Children fetching Method
public static void CreateChildNode(string TargetSiteServerURL, string SourceSiteServerURL, ClientContext SourceSiteCollectionctx, ClientContext TargetSiteCollectionctx, Web TargetWeb, NavigationNodeCollection LinkType, NavigationNodeCollection TargetLinksColl, NavigationNode ParentNodeName)
{
NavigationNodeCollection ChildrensNodeCollection = ParentNodeName.Children;
SourceSiteCollectionctx.Load(ChildrensNodeCollection);
SourceSiteCollectionctx.ExecuteQuery();
//// Display all the node title which is available in the top navigation bar
foreach (NavigationNode NavigationNodeChildren in ChildrensNodeCollection)
{
//// Get the collection of navigation nodes from the quick launch bar
NavigationNodeCollection nodes;
foreach (NavigationNode node in TargetLinksColl)
{
if (node.Title == ParentNodeName.Title)
{
nodes = node.Children;
try
{
// Describes a new navigation node to be created
NavigationNodeCreationInformation nodeCreation = new NavigationNodeCreationInformation();
//Tries for Adding with Target site Relative link, If error Catches and replaces with Server Link
nodeCreation.Title = NavigationNodeChildren.Title;
//Handles Relative URL
if (NavigationNodeChildren.Url.Trim() != "")
{
string NavLink = "";
if (NavigationNodeChildren.Url.StartsWith(RelativeStartLink))
{
NavLink = TargetSiteServerURL + NavigationNodeChildren.Url;
}
else
{
NavLink = NavigationNodeChildren.Url.Replace(SourceSiteServerURL, TargetSiteServerURL);
}
nodeCreation.Url = NavLink;
}
//// Add the new navigation node to the collection
nodeCreation.AsLastNode = true;
nodeCreation.IsExternal = NavigationNodeChildren.IsExternal;
nodes.Add(nodeCreation);
TargetSiteCollectionctx.Load(nodes);
TargetSiteCollectionctx.ExecuteQuery();
}
catch
{
// Describes a new navigation node to be created
NavigationNodeCreationInformation nodeCreation = new NavigationNodeCreationInformation();
//Tries for Adding with Target site Relative link, If error Catches and replaces with Server Link
nodeCreation.Title = NavigationNodeChildren.Title;
//Handles Relative URL
if (NavigationNodeChildren.Url.Trim() != "")
{
if (NavigationNodeChildren.Url.StartsWith(RelativeStartLink))
{
nodeCreation.Url = SourceSiteServerURL + NavigationNodeChildren.Url;
}
}
//// Add the new navigation node to the collection
nodeCreation.AsLastNode = true;
nodeCreation.IsExternal = NavigationNodeChildren.IsExternal;
nodes.Add(nodeCreation);
TargetSiteCollectionctx.Load(nodes);
TargetSiteCollectionctx.ExecuteQuery();
}
Console.WriteLine("Added Child Link - " + NavigationNodeChildren.Title);
}
}
CreateChildNode(TargetSiteServerURL, SourceSiteServerURL, SourceSiteCollectionctx, TargetSiteCollectionctx, TargetWeb, LinkType, TargetLinksColl, NavigationNodeChildren);
}
}
0 comments