Tuesday, March 13, 2012

Create a list, columns and view programmatically


Create a list, columns and view programmatically

Next task on my prep – create a list programmatically. Then I want to add some columns to it, and show these on the default view. Actually, it’s mostly pretty easy (apart from that last part).
Well, creating a list is pretty easy.
SPSite siteCol = new SPSite(@"http://vm-moss2007/test/");
SPWeb site = siteCol.OpenWeb();
site.Lists.Add("New Calendar", "This calendar was created programmatically", site.ListTemplates["Calendar"]);
Here you can see me getting an SPWeb object, and then adding to itsSPListCollection. You have to supply a template for the list, and this depends on the templates available on the site – hence getting the template from theSPListTemplateCollection.
I wanted a custom list, and I wanted it to appear on the left navigation:
site.Lists.Add("My Custom List", "This list was created programmatically", site.ListTemplates["Custom List"]);
SPList newList = site.Lists["My Custom List"];
newList.OnQuickLaunch = true;
newList.Update();
Great! And now I want to add some columns. First I added a text column:
newList.Fields.Add("NewText", SPFieldType.Text, true);
And then a lookup column (as I figured this would be a hard one):

SPList targetList = site.Lists["My Lookup Target"];
SPField targetField = targetList.Fields["My Target Lookup Column"];
newList.Fields.AddLookup("NewLookup", targetList.ID, false);
SPFieldLookup lkp = (SPFieldLookup)newList.Fields["NewLookup"];
lkp.LookupField = targetField.InternalName;
lkp.Update();
First I get the target list and the target field for the lookup. I add a new lookup column, but for some strange reason none of the SPFieldLookup class’s constructors allow me to create one supplying a target list and column. Also, none of the SPFieldCollection’s methods allow me to add a lookup supplying a target column.
Thus, I end up getting the column I’ve just created and assigning the column that I want use as the value of the lookup. Crazy. I’m sure that there must be a better way of doing this (i.e. in one call) but I don’t see how.
Anyway, it works; my columns are added. Now I want to show them in the default view:
//*** Doesn't work for some obscure reason ***
newList.DefaultView.ViewFields.Add("NewText");
newList.DefaultView.ViewFields.Add("NewLookup");
newList.DefaultView.Update();
As the code subtly hints, this code didn’t work – my columns weren’t shown in the default view. However, the code below does work:
SPView view = newList.DefaultView;
view.ViewFields.Add("NewText");
view.ViewFields.Add("NewLookup");
view.Update();

Friday, March 9, 2012

"Column Limit Exceeded"


Just run these power shall commands
By Default  MaxListItemRowStorage =6
$webApp = Get-SPWebApplication servername
$webApp.MaxListItemRowStorage = 8
$webApp.Update()

Thursday, March 8, 2012

SharePoint Web Application vs Site Collection vs Site vs Sub site


SharePoint Web Application vs Site Collection vs Site vs Sub site

Lately I'm playing a lot with WSS and MOSS. It wasn't really clear for me what the difference is between a web application, a site collection, a site and a sub site. This is what I found out. On top of the hierarchy is the web application. In technical terms, this is a web site in IIS (with an application pool associated). A web application needs to have at least 1 Site Collection. The Site Collection is the root site of the web site. Below the Site Collection, there can be one or more Sites. And a Site can contains sub sites.
An overview:
1. Web Application
1.1 Site Collection (SPSite)
1.1.1 Site (SPWeb)
1.1.2 Site (SPWeb)
1.2 Site Collection (SPSite)
1.2.1 Site (SPWeb)
1.2.1.1 Sub Site (SPWeb)
1.2.1.2 Sub Site (SPWeb)
The words between ( ) are the objects of the SharePoint API.
Backup
You cannot backup a web application from within SharePoint. But what you can do is save the configuration in IIS.
With stsadm.exe -o backup -url <site URL> -filename <filename> you can create a backup of a site and all sub sites. And you can restore it with stsadm.exe -o restore -url <site URL> -filename <filename>.
As far as I know there is no built-in option to backup or restore a sub site of SharePoint.
Web Applications
In my case I created three web applications. One for my private SharePoint, one for my blog (new blog soon) and another which allows anonymous access, but IP-filtered. Because each web application of SharePoint is a web application in IIS, you can setup all the security properties of IIS, like IP-based security.

PowerShell script to delete file versions from the specified SharePoint document library

Managing file versions in SharePoint Online is essential to maintain storage hygiene and performance, especially when versioning is enabled ...