You can create a document library with several document templates for example, excel, word, webpart page etc. If you need to create a document library with ‘web part page’ document template programmatically then you can do it without enabling publishing feature. Most of the examples on the web uses publishing feature.
Create a new document library with ‘web part page’ document template
The following code snippet shows how to Create the web part page document library.
In the CreateWebPartPageLibrary method, the first line get the template for document library. Then the GetWebPartPageDocTemplate return the document template for “web part page”. Finally I have called web.lists.add method to add our new list to the list collection.
Add a web part page in Web Part page library
Once you have web part page library, you need to add web part page in the library. You can do so programmatically. To add the page you need to prepare an xml command and execute with with ProcessBatchData method.
In the above code snippet a command is prepared to generate a web part page. In the command the Type is WebPartPage. WebPartPageTemplate is for different page layout that we can find in the sharepoint UI. The page title is only title without aspx.
Add web part to webpart page
You have created a ‘web part page’ library and added a webpart page. Now you need to add your web part in that page. First of all you need to create a web part instance. The following code Snippet shows how to create a web part programmatically.
The web part name in the above method GetWebPart() is like “employeeviewer.webpart”. The webpart collection exists in the rootweb. So I have check the IsRootWeb to make sure I run the caml query against the root web.
Once you have got the web part instance created by Activator.CreateInstance you can add it in the page.
The code snippet above call the GetWebPart() method to get the instance of the web part. Then using LimitedWebPartManager it get the page’s web part manager. Then it check if the web par already exists in the page by checking title. And finally add the web part if it doesn’t exists. FYI, be careful to dispose webpart and SPLimtiedWebPartManager.