Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically instantiate a web part page in Sharepoint

Is there a simple way to add a web part page to a Sharepoint site programmatically, using either the object model or web services? It seems straight-forward to create lists and add web parts in this manner, but I can't find an example of how to create a content page.

Edit: For a plain WSS installation (not MOSS).

like image 985
thade Avatar asked Jun 11 '09 06:06

thade


People also ask

What are WebParts in SharePoint?

Web parts are server-side controls that run inside a web part page: they're the building blocks of pages that appear on a SharePoint site. See Building Block: Web Parts. You can create and debug web parts on a SharePoint site by using templates from Visual Studio.


1 Answers

I'm going to take the route that this isn't a collaboration/publishing site as this isn't mentioned and wss is in the tag list. Pretty clunky in comparison to using a publishing site...

First choose the web part page template you'd like to use from:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\STS\DOCTEMP\SMARTPGS

Then set up a stream to the template and use SPFileCollection.Add() to add it to your document library. For example:

string newFilename = "newpage.aspx";
string templateFilename = "spstd1.aspx";
string hive = SPUtility.GetGenericSetupPath("TEMPLATE\\1033\\STS\\DOCTEMP\\SMARTPGS\\");
FileStream stream = new FileStream(hive + templateFilename, FileMode.Open);
using (SPSite site = new SPSite("http://sharepoint"))
using (SPWeb web = site.OpenWeb())
{
    SPFolder libraryFolder = web.GetFolder("Document Library");
    SPFileCollection files = libraryFolder.Files;
    SPFile newFile = files.Add(newFilename, stream);
}

Note: This solution assumes you have the US SharePoint version installed that uses the 1033 language code. Just change the path if different.

like image 126
Alex Angas Avatar answered Sep 23 '22 18:09

Alex Angas