Tuesday, February 9, 2010

List Tree View in Sharepoint


In one of our project there are more than 100 lists and every list is having nested folders as well as many items. My client don't want to dig the list to see all the nested folder inside it. So we decided to provide him the Tree View control which will show all the lists inside site and also show all the items/data inside a list and he can easily navigate to particular list/folder and item.

First we will show all the items/nested folder in TreeView

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Nishnat.ListTreeView
{
public class ListItems : WebControl
{
public string ListName { get; set; }

protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
// render the control
base.RenderContents(writer);
}

protected override void CreateChildControls()
{
base.CreateChildControls();

using (SPSite ospSite = new SPSite("http://localhost:7777/"))
{
using (SPWeb web = ospSite.RootWeb)
{
try
{
SPList list = web.Lists[ListName];

SPFolder rootFolder = list.RootFolder;
TreeView listTree = new TreeView();
listTree.ShowLines = true;
listTree.ExpandDepth = 0;

TreeNode rootNode = new TreeNode();

//Bind tree
MakeTreee(rootFolder, rootNode);

// add the root node to tree view
listTree.Nodes.Add(rootNode);
this.Controls.Add(listTree);
}
catch(Exception ex)
{
throw ex;
}
}
}
}

private void MakeTreee(SPFolder rootFolder, TreeNode rootNode)
{
SPQuery query = new SPQuery();
query.Folder = rootFolder;
SPWeb web = rootFolder.ParentWeb;
SPListItemCollection listColl = web.Lists[rootFolder.ParentListId].GetItems(query);

foreach (SPListItem subitem in listColl)
{
if (subitem.Folder != null) //Is folder
{
TreeNode childNode = new TreeNode(subitem.Folder.Name,subitem.ID.ToString(), "_layouts/images/folder.gif", subitem.Folder.ServerRelativeUrl, "");
rootNode.ChildNodes.Add(childNode);
MakeTreee(subitem.Folder, childNode);
}
else
{
string displayURL = GetDisplayUrl(subitem);

TreeNode childNode = new TreeNode(subitem.Name, subitem.Name, "", displayURL, "");
rootNode.ChildNodes.Add(childNode);
}
}
}

private static string GetDisplayUrl(SPListItem item)
{
SPList list = item.ParentList;
SPWeb web = list.ParentWeb;

string dispUrl = item.ContentType.DisplayFormUrl;
//dispUrl = String.Format("{0}/{1}?ID={2}", web, dispUrl, item.ID);

if (dispUrl == "")
dispUrl = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;

bool isLayouts = dispUrl.StartsWith("_layouts/", StringComparison.CurrentCultureIgnoreCase);
dispUrl = String.Format("{0}/{1}?ID={2}", web, dispUrl, item.ID);

if (isLayouts)
dispUrl = String.Format("{0}&List={1}", dispUrl, SPEncode.UrlEncode(list.ID + ""));
return dispUrl;
}
}
}


Second we will show all the Lists/items/nested folder in TreeView


using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Nishnat.ListTreeView
{
public class AllListsWithItems : WebControl
{
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
// render the control
base.RenderContents(writer);
}

protected override void CreateChildControls()
{
base.CreateChildControls();

using (SPSite ospSite = new SPSite("http://localhost:7777/"))
{
using (SPWeb web = ospSite.RootWeb)
{
try
{
TreeView listTree = new TreeView();
listTree.ShowLines = true;
listTree.ExpandDepth = 0;

SPListCollection listAll = web.Lists;

foreach (SPList list in listAll)
{
SPFolder rootFolder = list.RootFolder;

TreeNode rootNode = new TreeNode(list.Title,list.ID.ToString(),list.ImageUrl,list.ParentWebUrl,"");

//Bind tree
MakeTreee(rootFolder, rootNode);

// add the root node to tree view
listTree.Nodes.Add(rootNode);
this.Controls.Add(listTree);
}
}
catch(Exception ex)
{
throw ex;
}
}
}
}

private void MakeTreee(SPFolder rootFolder, TreeNode rootNode)
{
SPQuery query = new SPQuery();
query.Folder = rootFolder;
SPWeb web = rootFolder.ParentWeb;
SPListItemCollection listColl = web.Lists[rootFolder.ParentListId].GetItems(query);

foreach (SPListItem subitem in listColl)
{
if (subitem.Folder != null) //Is folder
{
TreeNode childNode = new TreeNode(subitem.Folder.Name,subitem.ID.ToString(), "_layouts/images/folder.gif", subitem.Folder.ServerRelativeUrl, "");
rootNode.ChildNodes.Add(childNode);
MakeTreee(subitem.Folder, childNode);
}
else
{
string displayURL = GetDisplayUrl(subitem);

TreeNode childNode = new TreeNode(subitem.Name, subitem.Name, "", displayURL, "");
rootNode.ChildNodes.Add(childNode);
}
}
}

private static string GetDisplayUrl(SPListItem item)
{
SPList list = item.ParentList;
SPWeb web = list.ParentWeb;

string dispUrl = item.ContentType.DisplayFormUrl;
//dispUrl = String.Format("{0}/{1}?ID={2}", web, dispUrl, item.ID);

if (dispUrl == "")
dispUrl = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;

bool isLayouts = dispUrl.StartsWith("_layouts/", StringComparison.CurrentCultureIgnoreCase);
dispUrl = String.Format("{0}/{1}?ID={2}", web, dispUrl, item.ID);

if (isLayouts)
dispUrl = String.Format("{0}&List={1}", dispUrl, SPEncode.UrlEncode(list.ID + ""));
return dispUrl;
}
}
}


* Add assembly into GAC and Bin application

* Register the control on the page


<@ Register Tagprefix="Nishnat" Namespace="Nishnat.ListTreeView" Assembly="Nishnat.ListTreeView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=57c7737278844f55">


* Now Use it on the page

<Nishnat:ListItems Id="listItems" runat="server" ListName="Books">
</Nishnat:ListItems>
<Nishnat:AllListsWithItems Id="AllListsWithItems" runat="server">
</Nishnat:AllListsWithItems>

Thursday, June 4, 2009

Rename title field of list programatically in Sharepoint


//Get the site
SPWeb web = SPContext.Current.Web;

// Get the list
SPList list = web.Lists["List Name"];
web.AllowUnsafeUpdates = true;

list.Fields["Title"].Title = "New Title";
list.Fields["Title"].Description = "This is description of field.";
list.Fields["Title"].Update(); //Don't forget to update list field

//Update list
list.Update();
web.AllowUnsafeUpdates = false;

Create a list programatically in sharepoint

Hi friends
Here i will show u how to create list of type Custom List.


SPWeb web = SPContext.Current.Web; //Get current site
string listName = "My New List";

try
{
//We r purposely putting it in try/catch block because if list not present in the site then code will throw an exception not return null.
SPList oldList = web.Lists[listName]; // Check if list is exist
}
catch (ArgumentException ex)
{
web.AllowUnsafeUpdates = true;
// Create a list
Guid listGuid = web.Lists.Add(listName, "This is my first custom list.", SPListTemplateType.GenericList);

// Set list properties
SPList list = web.Lists[listGuid];
list.OnQuickLaunch = true;

web.AllowUnsafeUpdates = true;
}

You can create any type of list by changing list template (i.e- SPListTemplateType.GenericList ).