开发者

SharePoint - Custom document library with folder structure

I have a custom document library template with content types. This works fine but the only thing that I would like to add is that when a user instantiates a new document library based on that template, that is has a predefined folder structure already in place.

I have tried adding Module tags in my schema.xml but this doesn't seem to work.

I know that it is possible to provision a document library with f开发者_运维问答iles and folders with a ListInstance feature but this is not possible in this case. I would like that the predefined folder structure is part of the document library template.

Is this possible?

Thanks

Maarten


If You want to achieve this using Document Library Definition. I don't think that would be achievable. What you can do is take help of list /document library templates. 1 Create a custom Doclibary the way you want. 2. create the desired folder structure. without uploading any documents. 3, Save the doc library as template by going to Doclibray settings ( make sure you store the template along with content stored into it)


Another method (which I must blog on soon) is to fake a list creation event. I add an empty view definition with a custom aspx page to the list template. The custom page simply executes some custom functionality on the list, deletes the initialisation view, then redirects to the normal view. It's a little messy, and it will only work if the list is created through the UI, but it works.

Here is a very quick example. You already have your list template. In the schema.xml file, add a new View to the Views element like so:

<Views>
  <!-- Below is a blank view used to kick of initialisation after list creation. -->
  <View DisplayName="Initialise" Type="HTML" DefaultView="TRUE"  WebPartZoneID="Main" SetupPath="pages\Scratch\init.aspx" Hidden="TRUE" Url="_init.aspx">
    <Toolbar Type="Standard" />
    <ViewHeader />
    <ViewBody />
    <ViewFooter />
    <ViewEmpty />
    <ViewFields />
    <ViewData />
    <Query />
  </View>
  <!-- standard views would be here -->
</Views>

You may be able to go without the empty elements in there. That was something I was going to test further before blogging on it. But this will get the job done. The important things are:

  • This view is the first view and DefaultView is set to TRUE.
  • The SetupPath is set to a custom page that you will provision with your solution.

For the custom page (init.aspx in my example), I just made a copy of ...\12\TEMPLATE\Pages\viewpage.aspx and changed what the page inherits from. You could do this with inline code, but I used a codebehind assembly. So first line of that file becomes:

<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="SharePointScratch.InitPage,SharePointScratch,Version=1.0.0.0,Culture=neutral,PublicKeyToken=xxxxxxxxxxxxxxxx" %>

And then the codebehind:

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SharePointScratch
{
    public class InitPage : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            SPList list = SPContext.Current.List;
            list.ParentWeb.AllowUnsafeUpdates = true;

            // Create you folder structure here...

            // Fix the views by deleting the initialisation view.
            SPView view = SPContext.Current.ViewContext.View;
            list.Views.Delete(view.ID);
            list.Update();

            list.ParentWeb.AllowUnsafeUpdates = false;

            // Redirect to the new default view.
            SPUtility.Redirect(list.DefaultViewUrl, SPRedirectFlags.Default, this.Context);
        }
    }
}

Basically, we are relying on the SharePoint default behavior to display the default view of a list after creation. A custom view is inserted in the schema with the sole intention of firing off some custom code. The custom code does, well, whatever you want. After this, you clean up by deleting the special view from the template and redirecting back to the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜