0

This is a good question with no good answer.

I have HTML I want to inject into various existing SharePoint views, via powershell. I've been doing this via SharePoint designer so far - is there a way to automate this?

I can't find the relevant API, though I'm sure there's a way to do it. After all, SharePoint designer does it!

$web = Get-SPWeb "http://sp.caritas.local/sites/crm/hr"
$list = $web.Lists["Contracts"]

$list.Views["All Documents"].HtmlSchemaXml

The below gives me XML like the following.

<View Name="{127149EA-D867-4E3A-BBF2-9A6A62678E53}" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" Type="HTML" DisplayName="All Documents" Url="/sites/crm/hr/Contracts/Forms/AllItems.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl=
"/_layouts/15/images/dlicon.png?rev=23"><XslLink Default="TRUE">main.xsl</XslLink><JSLink>clienttemplates.js</JSLink><RowLimit Paged="TRUE">30</RowLimit><Toolbar Type="Standard" /><ViewFields><FieldRef Name="DocIcon" /><FieldRef Name="LinkFilename" /><Field
Ref Name="Modified" /><FieldRef Name="Editor" /></ViewFields><ParameterBindings><ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noitemsinview_doclibrary)" /><ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noitemsinview_d
oclibrary_howto2)" /></ParameterBindings><Query><OrderBy><FieldRef Name="FileLeafRef" /></OrderBy></Query></View>

Is there a way to set/get the view HTML?

5
  • SPView.HtmlSchemaXml seems to be a readonly property. msdn.microsoft.com/en-us/library/… Commented Mar 13, 2015 at 15:06
  • @NadeemYousuf It doesn't even matter. The field is not what I'm looking for - it does not provide the HTML. Commented Mar 13, 2015 at 15:09
  • David, do you simply want to inject html on the AllItems.aspx page which happnes to be a view page? Commented Mar 13, 2015 at 15:13
  • Do you want to modify something in list view or do you just want to add some html to the page that is hosting the list view, I.e some instructions? Commented Mar 13, 2015 at 23:55
  • @SimonDoy I want to add some html to the page that is hosting the view, like in sharepoint designer, Commented Mar 15, 2015 at 11:45

2 Answers 2

0

This is something that I knocked up on the train.

To use it, create a content editor web part with your html that you wish to add to the list views and export that to your file system.

Then run it providing the url to the web, name of the list to add the web part to the views of and the path to the web part that you have created and exported out.

Hope that helps. There are a few cavaets it works but has not been hardened and it will add a web part if it already exists on the page. Also you will need some tweaking to get it to appear in the right zone etc but should give you a good start.

param
(
    [Parameter(Mandatory=$false, HelpMessage='Url of site containing list')]
    [string]$SiteUrl="http://sharepoint",
    [Parameter(Mandatory=$false, HelpMessage='Name of List')]
    [string]$ListName="Workflow Tasks",
    [Parameter(Mandatory=$false, HelpMessage='Web Part file path')]
    [string]$WebPartFilePath="Content Editor.dwp",
    [Parameter(Mandatory=$false, HelpMessage='Name of the web part zone')]
    [string]$WebPartZone="Top",
    [Parameter(Mandatory=$false, HelpMessage='order of web part to load into')]
    [int]$WebPartIndex=0    

)

asnp Microsoft.SharePoint.PowerShell;

$site=Get-SPSite -Identity $SiteUrl -ErrorAction SilentlyContinue;
if($site -ne $null)
{
    $web = $site.RootWeb;

    $list = $web.Lists.TryGetList($ListName);

    $fileStream = [System.IO.File]::OpenRead($WebPartFilePath);

    if($fileStream -ne $null)
    {
        $importErrorMessage="";



        foreach($listView in $list.Views)
        {
            #move stream to the start of the flie
            $fileStream.Seek(0,[System.IO.SeekOrigin]::Begin);
            $xmlReader = [System.Xml.XmlReader]::Create($fileStream);

            Write-Host "Updating List View $listView.Url with Web Part $WebPartFilePath";
            $listViewPageUrl = $web.Url + $listView.Url;
            $listViewPage = $web.GetFile($listViewPageUrl);

            $limitedWebPartManager = $listViewPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
            $webPartObject = $limitedWebPartManager.ImportWebPart($xmlReader, [ref]$importErrorMessage);
            $limitedWebPartManager.AddWebPart($webPartObject, $WebPartZone, $WebPartIndex); 
            $limitedWebPartManager.SaveChanges($webPartObject);
            $listViewPage.Update();
            Write-Host "Done" -ForegroundColor Green;

        }

        $fileStream.Close();
    }

}
3
  • I think this might help. I'm wondering if I can directly update the listViewPage and inject HTML into it. Commented Mar 16, 2015 at 12:54
  • @David - if the list view page was a wiki page which had a field for holding HTML content then that would be the case. Unfortunately it isn't but by adding a web part to the page you are achieving the same thing. Commented Mar 16, 2015 at 22:26
  • Thank you for your help! I'm going to try it out today hopefully Commented Mar 18, 2015 at 9:13
0

The good question no anwers link you include are all about pre-SP2013 solutions, but one mentions CSR

CSR/JSlink is the SP2013 way; see http://www.idubbs.com/blog/category/sharepoint/spc14/

As an alternative you can stuff HTML/Javascript in Calculated Columns, powerfull but a wacky concept
see https://www.365csi.nl/vm365com/#/How

1
  • Thanks! However JSLink does not allow you to modify the ASP code. One is client and the other is server side. Commented Mar 13, 2015 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.