Archive

Posts Tagged ‘Programming’

Create a list in all the sites using Powershell

September 20, 2011 3 comments

Here is way to create a list based on a custom template using Powershell. The script will go through each site and create the list and add them to the Quick Launch


$GetSites = Get-SPWebApplication | Get-SPSite | Get-SPWeb -Limit ALL
foreach($getsite in $GetSites)
{
$listTemplate = $getsite.Site.GetCustomListTemplates($getsite)
$getsite.Lists.Add("MyList","My List",$listTemplate["MyTemplate"])
$list = $getsite.Lists["MyList"]
$list.OnQuickLaunch = $true
$list.Update()

}

Make sure that custom template is uploaded in the List Template Gallery. Check out this link  if you want to add a list template using powershell

Advertisements

How to create Views Programmatically in SharePoint 2010

August 17, 2011 2 comments

Below is the code snippet to create views programmatically

Create a View

      private void CreateView(string strViewName)
{
try
{
string fieldName = //Get Field Internal Name
var docquery = "<Where><Eq><FieldRef Name='" + fieldName.ToString() + "' /><Value Type='Choice'>" + strViewName.ToString() + "</Value></Eq></Where>";
System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection();
viewFields.Add("Type");
viewFields.Add("Name");
viewFields.Add("Modified");
viewFields.Add("Modified By");
viewFields.Add(fieldName.ToString());
oViewCollection.Add(strViewName, viewFields, docquery, 100, true, false);
web.Update();
}
catch (Exception e)
{
throw new SPException(e.Message.ToString());
}
}

You can use U2U Query Builder tool to build CAML query (http://www.u2u.net/res/Tools/CamlQueryBuilder.aspx)

 

Web.config Alternatives

February 26, 2011 Leave a comment

Good resource for alternatives of storing configuration data in web.config

http://msdn.microsoft.com/en-us/library/ee413935.aspx