Call AuthorizeWebParts in codebehind

Last post 06-29-2009 9:49 PM by EDUStreet. 2 replies.

Sort Posts:

  • Call AuthorizeWebParts in codebehind

    06-24-2009, 12:28 AM
    • Member
      404 point Member
    • EDUStreet
    • Member since 10-03-2003, 1:52 AM
    • Posts 122

    My code check each webpart in DeclarativeCatalogPart and assign the role for them. But it doesn't run the after the DeclarativeCatalogPart1_Init life cycle. Do know how to solve it? I want all webpart "Refresh" after assigned role. Appreciate if provide any solution.  Thank you.

    ----------------------------------------------------------------------------------------------------- 
    Default.aspx.vb 

    Protected Sub DeclarativeCatalogPart1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeclarativeCatalogPart1.Init 
         Dim WebPartManager1 As WebPartManager = Master.FindControl("WebPartManager1") 

         For Each c As WebPartDescription In Me.DeclarativeCatalogPart1.GetAvailableWebPartDescriptions 
                Dim namingContainerID As String = "gwp" 
             If c.ID = namingContainerID + "Announcement1" Then 
                Dim wp As WebPart = DeclarativeCatalogPart1.GetWebPart(c) 
                wp.AuthorizationFilter = "admin" 
            End If 
         Next

    '************************************************************ 
    'Here call OnAuthorizeWebPart="AuthorizeWebParts" to refresh?
    '************************************************************ 
    End Sub 

    ----------------------------------------------------------------------------------------------------- 
    Default.aspx 
     
        <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server"/>
        <WebPartsTemplate> 
               <uc1:Announcement title="Announcement" runat="server" /> 
        </WebPartsTemplate> 
        </asp:DeclarativeCatalogPart>
    ----------------------------------------------------------------------------------------------------- 
    MyMaster.master 
       
        <asp:WebPartManager OnAuthorizeWebPart="AuthorizeWebParts" runat="server"> </asp:WebPartManager>
    ----------------------------------------------------------------------------------------------------- 
    MyMaster.master.vb 

    Protected Sub AuthorizeWebParts(ByVal sender As Object, ByVal e As WebPartAuthorizationEventArgs) 
      ' If no roles are set, simply exit 
       If String.IsNullOrEmpty(e.AuthorizationFilter) Then 
          Return 
       End If 

       Dim rolesArray As String() = e.AuthorizationFilter.Split(New Char() {";"c}) 

      ' Assume the user does not have access to the web part
        e.IsAuthorized = False 

       ' Loop until the first role that allows the user access to the 
       ' web part is encountered. 

       For Each roleStr As String In rolesArray 
            roleStr.Trim() '** I predefined the Session("Role")="user" in page_load for testing purpose ** 
                   If roleStr = Session("Role") Then 
                         ' the user has the rights, so authorize them 
                         e.IsAuthorized = True 
                         Exit For 
                   End If 
       Next roleStr 
    End Sub

  • Re: Call AuthorizeWebParts in codebehind

    06-29-2009, 12:52 AM
    Hong-Gang Chen
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Call AuthorizeWebParts in codebehind

    06-29-2009, 9:49 PM
    • Member
      404 point Member
    • EDUStreet
    • Member since 10-03-2003, 1:52 AM
    • Posts 122

    Thanks for reference, but it looks like unuseful for me. 

Page 1 of 1 (3 items)