Wednesday, April 18, 2007

Creating a document library with a view with grouping in MOSS programmatically

For this project I'm on at the moment, a document library needed to be created programmatically. It needed a view and inside the view, a grouping had to be made.

To do this, I found some info on the various steps on some blogs, msdn, ... but I hardly found anything about grouping. This is why I thought it would be useful to post a code example of the whole thing: creating a document library, creating a view on it and creating a grouping.

The code is fairly simple once you know it, so I won't go any deeper into it. If you've got any questions though, feel free to ask!

WebToAddLibraryTo.AllowUnsafeUpdates = true;

SPListTemplate listTemplate = WebToAddLibraryTo.ListTemplates["Document Library"];

System.Guid guidLib = WebToAddLibraryTo.Lists.Add("Title", "Description", listTemplate);
WebToAddLibraryTo.Update();

SPList docLib = WebToAddLibraryTo.Lists[guidLib];

docLib.EnableVersioning = true;
docLib.EnableMinorVersions = true;
docLib.OnQuickLaunch = true;

docLib.Fields.Add("Field name", SPFieldType.Text, true);
docLib.Fields.Add("Another field name", SPFieldType.Text, false);
docLib.Fields.Add("Date field", SPFieldType.DateTime, false);
StringCollection resultStrings = new StringCollection();
resultStrings.Add("Positive");
resultStrings.Add("Negative");
docLib.Fields.Add("Result", SPFieldType.Choice, false, false, resultStrings);

StringCollection viewFieldCollection = new StringCollection();

viewFieldCollection.Add("DocIcon");
viewFieldCollection.Add("LinkFilename");
viewFieldCollection.Add("Another field name");
viewFieldCollection.Add("Date field");
viewFieldCollection.Add("Result");

string defaultQuery ="<GroupBy Collapse=\"TRUE\" GroupLimit=\"100\">" +
"<FieldRef Name=\"Field name\">" +
"<FieldRef Name=\"ContentType\">" +
"</GroupBy>" +
"<OrderBy>" +
"<FieldRef Name=\"LinkFileName\">" +
"</OrderBy>";


Guid oldViewId = docLib.DefaultView.ID;
docLib.Views.Delete(oldViewId);

SPView newView = docLib.Views.Add("New view", viewFieldCollection, defaultQuery, 100, true, true, SPViewCollection.SPViewType.Html, false);

docLib.DefaultView.Update();

docLib.Update();

7 comments:

Anonymous said...

Hey u. Nice post u wrote! =D

First: i'm Filipe, Brazilian and i'm .net/c# (wss/moss) developer too...

Hum... i dont know if u can help me. I'm using SPGridView to show data from list. And now i dont know how, or if it's possible to do, but i need to set multiples groups on my spgridview.
Do u know or ever heard about how to do that?

(i'm sorry about my english... just learning yet) =D

well... you can visit my blog too, it is about Sharepoint too... (but in portuguese lol)...

thx anyway!
cya!

Tom said...

Hi Filipe, glad to hear you liked my post!

I haven't got any experience with multiple grouping on an SPGridView... I know that for a single grouping, you need the following lines:

gridView.AllowGrouping = true;
gridView.AllowGroupCollapse = true;
gridView.GroupField = "GroupField";
oGrid.GroupDescriptionField = "Description";
oGrid.GroupFieldDisplayName = "DisplayName";

I've done some quick googling and apparently having multiple groupings isn't that easy... So I hope you'll have more luck with it than those other people...

And by the way, your English is quite good... Way better than my Portuguese! :-)

Andy Sheppard said...

I have had a good search around for programmatically grouping in SPViews and found absolutely nothing until I stumbled upon your post.

So many thanks, your example makes it very clear.

Tom said...

Glad I could help!

Ankush said...

do not u think the field ref tag needs ti be closed.

Tom said...

Hi Ankush, good point... I never noticed that myself. I copied that part from someone else and it works, so that's why I never noticed it... so I guess you don't really need to close them for it to work propertly, but it's always nicer to do so of course.

Ankush said...

HI Tom
i was having issues without it hence i pointed it out. But great post.