New year, old tricks

Okay I haven’t been quite active lately on this blog. But here’s something that I have been digging and dugging lately: creating SharePoint 2007 (both WSS 3.0 and MOSS) web’s, lists and web parts programmatically. Now at the first, why in earth I am doing this? I have faced couple times nasty bugs with using templates and / or Custom Site Definitions with SharePoint 2007 deployment (such as reader’s do not see any documents and language problems that lead to datetime problems).

Goal of this digging is to be able create web with custom lists and web parts programmatically by using SharePoint 2007 API. And I need to raise my hat towards Microsoft. They have done a huge amount of stuff in SharePoint considering even this scenario. You can create almost everything from code in SharePoint (honestly I haven’t found any stuff that could not be done programmatically).

And here is a small tasty nice little code that creates a instance of a web part and adds it to designated page without XML stuff (personally I am not too eager to use XML and I always try to find ways do stuff without it):

SPWeb createdWeb = (yourDesignated SPWeb object);

(yournamespace.foryourwebpart).webpartClass webpartToMainPage = new (yournamespace.foryourwebpart).webpartClass();

 SPLimitedWebPartManager manager = createdWeb.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);

manager.AddWebPart(webpartToMainPage, "Left", 0)

I hope that someone gets a new thoughts with this small code.

Advertisements

About Sakarih

I am SharePoint developer/architect from Finland and my blogposts are all about SharePoint 2007/2010/2013 stuff. There might be also something else posted.
This entry was posted in SharePoint. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s