2

I know you can set the view to use on a Document Sets welcome page through SharePoint. But is there a way to do this in a project, like either in the content type Elements.XML or the libraries schema or the provisioning of the web parts on the welcome page? Creating a view for the content type in the list definition Schema.xml seems to override use of the welcome page at all.

1 Answer 1

3

You can do it through the document set template. Every document set based content type has it's own "template" (in addition to the content type definition). Here's the MSDN documentation on the document set template class.

Now, I don't know if you can do it declaratively through the Elements.xml files. The way I made it work was through code in a feature receiver.

Here is basically the process I went through to set the welcome page view through code:

public class MyCustomViewInstallerEventReceiver : SPFeatureReceiver
{
    private string myCustomViewName = "Group by My Custom Column";

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb web = properties.Feature.Parent as SPWeb;

        SPList list = web.Lists["List Where I Want To Install The View"];

        SPView myCustomView = null;

        // the view is actually installed on the SPList object
        // so first I check to see if the list already has the view
        // if not, I create a new view on the list
        try
        {
            myCustomView = list.Views[myCustomViewName];
        }
        catch (ArgumentException ex)
        {
            if (ex.Message.Contains("view is invalid"))
            {
                myCustomView = CreateMyCustomView(list);
            }
            else
                throw;
        }

        SPContentType cType = list.ContentTypes["My Custom Document Set"];

        bool readOnlySetting = cType.ReadOnly;
        cType.ReadOnly = false;
        cType.Update();

        DocumentSetTemplate docSetTemplate = DocumentSetTemplate.GetDocumentSetTemplate(cType);

        SetMyCustomViewOnWelcomePage(docSetTemplate, myCustomView);

        cType.ReadOnly = readOnlySetting;
        cType.Update();
    }

    private void SetMyCustomViewOnWelcomePage(DocumentSetTemplate docSetTemplate, SPView myCustomView)
    {
        try
        {
            docSetTemplate.WelcomePageView = myCustomView;
            docSetTemplate.Update(true);
        }
        catch (Exception ex)
        {
            // log the error here and then:
            throw;
        }
    }

    private SPView CreateMyCustomView(SPList list)
    {
        SPView myCustomView = null;

        try
        {
            StringCollection viewFields = new StringCollection();
            viewFields.Add("DocIcon");
            viewFields.Add("LinkFilename");
            viewFields.Add("Author");
            viewFields.Add("Created");
            string query = "<GroupBy Collapse=\"TRUE\"><FieldRef Name=\"MyCustomColumn\"/></GroupBy>";
            myCustomView = list.Views.Add(myCustomViewName, viewFields, query, 30, true, false, SPViewCollection.SPViewType.Html, false);
        }
        catch (Exception ex)
        {
            // log the error here and then:
            throw;
        }

        return myCustomView;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.