none
Set Target Audience for List View Web Part using Powershell / CSOM RRS feed

  • Question

  • Hi All,

    I have a list view. 

    I am setting target audience for that view in this way -

    Edit Page -> Edit WebPart -> Advanced --> Target Audiences --> Distribution(Security Groups) -->

    I am setting target audience for a particular list view.

    I want to do the same programatically, using csom powershell.

    Please suggest a way to achieve this.

    Note: I am creating this view ussing PS CSOM code. I just want to set the web part target audience

    Thanks

    Saturday, September 26, 2015 11:13 AM

Answers

  • Hi All,

    I could achieve this using below code. Just sharing so that it will be useful for someone.

            $List = $context.Web.Lists.GetByTitle("ListName")
            $context.Load($List)
            $context.ExecuteQuery()

        
            $Pages = $List.RootFolder.Files
            $context.Load($Pages)
            $context.ExecuteQuery()

            $Page = $Pages | Where {$_.Name -eq "ViewName"}
            $context.Load($Page)
            $context.ExecuteQuery()

            
            $WPM       = $Page.GetLimitedWebPartManager("Shared")
            $context.Load($WPM)
            $context.ExecuteQuery()

            $WebParts = $WPM.WebParts
            $context.Load($WebParts)
            $context.ExecuteQuery()

            $WebPart = $WebParts[0]
            $context.Load($WebPart)
            $context.ExecuteQuery()

            $WebPart.WebPart.Properties["AuthorizationFilter"] = "My Group"

            $WebPart.SaveWebPartChanges();
            $context.Load($WebPart);
            $context.ExecuteQuery();

    Thanks

    • Marked as answer by SPOL_365 Monday, September 28, 2015 5:57 AM
    Monday, September 28, 2015 5:57 AM

All replies

  • Hi All,

    I could achieve this using below code. Just sharing so that it will be useful for someone.

            $List = $context.Web.Lists.GetByTitle("ListName")
            $context.Load($List)
            $context.ExecuteQuery()

        
            $Pages = $List.RootFolder.Files
            $context.Load($Pages)
            $context.ExecuteQuery()

            $Page = $Pages | Where {$_.Name -eq "ViewName"}
            $context.Load($Page)
            $context.ExecuteQuery()

            
            $WPM       = $Page.GetLimitedWebPartManager("Shared")
            $context.Load($WPM)
            $context.ExecuteQuery()

            $WebParts = $WPM.WebParts
            $context.Load($WebParts)
            $context.ExecuteQuery()

            $WebPart = $WebParts[0]
            $context.Load($WebPart)
            $context.ExecuteQuery()

            $WebPart.WebPart.Properties["AuthorizationFilter"] = "My Group"

            $WebPart.SaveWebPartChanges();
            $context.Load($WebPart);
            $context.ExecuteQuery();

    Thanks

    • Marked as answer by SPOL_365 Monday, September 28, 2015 5:57 AM
    Monday, September 28, 2015 5:57 AM
  • Hi,

    Thanks for sharing!

    Best regards,

    Aaren liu

    Monday, September 28, 2015 10:33 AM