Skip to content

Instantly share code, notes, and snippets.

@brennanneoh
Created February 4, 2012 02:13
Show Gist options
  • Save brennanneoh/1734470 to your computer and use it in GitHub Desktop.
Save brennanneoh/1734470 to your computer and use it in GitHub Desktop.
sharepoint snippets
//obtaining reference to the current web (i.e. the web site which was defined when setting up the project in Visual Studio (in our case http://win7:8000/sites/QATP1PTS/)
objCurrentWeb = SPContext.Current.Web;
//Using SharePoint object model to retrieve both lists
objMembersList = objCurrentWeb.Lists["Project Members"];
objContactsList = objCurrentWeb.Lists["Project Contacts"];
//looping through the Members list
foreach (SPListItem objMembersListItem in objMembersList.Items)
{
//if the Job title is "Country Representative"
if (Convert.ToString(objMembersListItem["Job Title"]) == "Country Representative")
{
//create a top level node for the treeview control
TreeNode objMainTreeNode = new TreeNode();
//the text property of the node will have the following format: Carlos Gomez (Mexico) - FirstName LastName (country)
objMainTreeNode.Text = objMembersListItem["First name"].ToString() + " "
+ objMembersListItem["Last name"].ToString()
+ " (" + objMembersListItem["Representing"].ToString() + ")";
//the value property of the node will be eqal to the country this person is representing
objMainTreeNode.Value = objMembersListItem["Representing"].ToString();
//the new node is added to the nodes collection of the treeview control
trvContacts.Nodes.Add(objMainTreeNode);
}
}
foreach (SPListItem objContactsListItem in objContactsList.Items)
{
//for each contact look through the top level nodes in the treeview control and determine under which representative that particular contacts goes
foreach (TreeNode objMainTreeNode in trvContacts.Nodes)
{
//if the country of the contact is equal to the county of the representative
if (objContactsListItem["Country"].ToString() == objMainTreeNode.Value)
{
//a new node under that top level node is created
objSubTreeNode = new TreeNode();
//the text property of the contact node will have the following format: Juan Garcia (IBM) - FirstName LastName (company)
objSubTreeNode.Text = objContactsListItem["First name"].ToString() + " "
+ objContactsListItem["Last name"].ToString() +
" (" + objContactsListItem["Company"].ToString() + ")";
//the value property of the contact node will have the following format: FirstName$LastName$company (the $ sign is used as separator)
objSubTreeNode.Value = objContactsListItem["First name"].ToString() + "$"
+ objContactsListItem["Last name"].ToString() + "$"
+ objContactsListItem["Company"].ToString();
//the new node is added to the ChildNodes collection of the top level node
objMainTreeNode.ChildNodes.Add(objSubTreeNode);
break;
}
}
}
trvContacts.ExpandAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment