开发者

Manage views for content types (or add a view to multiple lists)

I'm surprised that I cannot find on the net a solution where I could manage views for content types. Do I really have to visit each task l开发者_JAVA技巧ist with my browser to add/modify a view?

Is there any solutions available that would allow me to just define a view for content type and thus make this view available on all lists where content type is?


Directly speaking, no, a view cannot be assigned to a content type. Lists are what hold an actual view collection, and the corresponding aspx page that is created with it. A view also has a dependent existence with its list: you cannot have a view that is not associated with a list. There is also no "event handler" for adding a content type to a list, either, so you can't have some automatic process that occurs whenever you add a content type to the list (and it would be cumbersome anyway as you'd have to attach the event handler to the list in the first place!).

This isn't to say you have to manually muck about in the UI to accomplish the task, though. Using the object model in something like a custom code workflow, you can sweep across your entire SPWeb and create a new view on each list that has the specified content type. All you need is a workflow with a single code activity, which iterates across all of the SPLists in SPWeb.Lists, and check if that SPList has the content type with the same name as your target content type. If it does, create a new SPView with the parameters you need. Since you cannot simply create one SPView and clone it, as an SPView must be associated with a list and cloning an SPView just clones it onto the same list, you'll have to run the whole SPView creation in each loop. But you only have to write it once, it's the system that'll have to run it multiple times. And by heavens, it'll certainly get that done a lot more productively than if you had to dance about in the UI for a few hours.

Simply run the workflow any time you need to reassert the existence of that SPView.


I found this solution in c#, however I have not yet tested it. I will test it in the future, and update this if necessary.

Apparently, it is for SharePoint 2010, however it may work in later versions too.

private void CreateView(string strViewName)
{
    try
    {
        string fieldName = //Get Field Internal Name
        var docquery = "<Where><Eq><FieldRef Name='" + fieldName.ToString() + "' /><Value Type='Choice'>" + strViewName.ToString() + "</Value></Eq></Where>";
        System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection();
        viewFields.Add("Type");
        viewFields.Add("Name");
        viewFields.Add("Modified");
        viewFields.Add("Modified By");
        viewFields.Add(fieldName.ToString());
        oViewCollection.Add(strViewName, viewFields, docquery, 100, true, false);
        web.Update();
    }
    catch (Exception e)
    {
        throw new SPException(e.Message.ToString());
    }
}

I also found this solution in PowerShell...

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA silentlycontinue

#Title View
$viewTitle = "Sort by modified date"
#Add the column names from the ViewField property to a string collection
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add("DocIcon") > $null
$viewFields.Add("LinkFilename") > $null
$viewFields.Add("Modified") > $null
$viewFields.Add("Editor") > $null
$viewFields.Add("FileSizeDisplay") > $null
#Query property
$viewQuery = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE'/></OrderBy>"
#RowLimit property
$viewRowLimit = 50
#Paged property
$viewPaged = $true
#DefaultView property
$viewDefaultView = $false

$ListsToUpdate = @()

$App = Get-SPWebApplication  http://....
foreach ($Site in $App.Sites)
{
    foreach ($Web in $Site.AllWebs)
    {
        foreach ($List in $Web.Lists)
        {
            if($List.BaseType -eq "DocumentLibrary" -and $List.Title -eq "Documents" )
            {
                $ListsToUpdate += $Web.Lists[$List.Title]
            }
        }
    }
}


foreach($List in $ListsToUpdate) 
{
    Write-Host $List.Title
    #Create the view in the destination list
    $newview = $List.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜