Friday, February 24, 2012

Create Grid View

private static string CreateGridView(SPWeb web, string listName, string viewName, string fields)
{
SPList list = web.Lists.TryGetList(listName);
if (null == list)
{
// LogError
return string.Empty;
}
System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection();
string[] names = fields.Split(';');
foreach (string name in names)
{
viewFields.Add(name);
}
SPView view = list.Views.Add(viewName, viewFields, "", 100, false, false, SPViewCollection.SPViewType.Grid, false);
return view.Url;
}

Add to Quick Launch

private static void AddToQuickLaunch(SPWeb web, string viewUrl, string viewTitle)
{
if ((string.IsNullOrEmpty(viewTitle))
(string.IsNullOrEmpty(viewUrl)))
{
return;
}
SPNavigationNodeCollection nodes;
SPNavigationNode node;
nodes = web.Navigation.QuickLaunch;
node = new SPNavigationNode(viewTitle, viewUrl);
nodes.AddAsFirst(node);
}