locked
Public/Private Web Part? RRS feed

  • Question

  • How can I change a WebPart to Public/Private? If user made as "Public" everybody can see that. If he made a "Private", he can only see that.


    Photobucket
    Jamsbond
    Tuesday, November 3, 2009 5:04 PM

Answers

  • You can enable personalisation in MOSS http://technet.microsoft.com/en-us/library/cc263073.aspx#section3 .  This would allow users to customise the page and add webparts that will only be shown to them (not shared).

    Dave
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Tuesday, November 3, 2009 5:17 PM
  • How do you define private?  What user should see the webpart?  The user who added the webpart?

    A simple way would be to have two webpart properties: 1. for the type (ie private or public), 2. for the user who added the webpart.  Then in the render check the if the webpart is private, if it is then check the current user is the value stored in the second webpart property.  If it fails these validation rules then don't render.  You might have a problem with webpart chrome, but if you use the render method this may be able to suppress the chrome or failing that add some css to hide the webpart.
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Tuesday, November 3, 2009 6:00 PM
  • I would still use the same approach just you don't need the second property to store the user name.  You then need to perform the check against the User Profile for the current user and find out if its their mysite.
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Wednesday, November 4, 2009 9:27 AM
  • Hi,

    You can show/hide webpart based on public/private property values with the help of helper webpart, use the below code snippet which has a property where you can set the value as 'Public' or 'Private' in webpart properties area, if you are looking for whenever user clicks on link 'Make Public' you can change the webpart property value to 'Public' by adding a link button and try to change the value in link button on click event


    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Serialization;
    using System.Web.UI.WebControls;
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    
    namespace MSDN
    {
        [Guid("30cfdaad-921d-45ca-ad86-144d11b2a156")]
        public class MSDN : System.Web.UI.WebControls.WebParts.WebPart
        {
            private string _Status = String.Empty;
    
            [WebBrowsable(true),
            Personalizable(PersonalizationScope.User),
            WebDescription("Public/Private"),
            Category("Public/Private"),
            WebDisplayName("Make Public/Private")]
            public string VisibleStatus
            {
                get
                {
                    return _Status;
                }
                set
                {
                    _Status = value;
                }
            }
            
            protected override void Render(HtmlTextWriter writer)
            {          
                SPWeb web = SPControl.GetContextWeb(Context);
                writer.Write(VisibleStatus);
                try
                {
                    if (VisibleStatus == "Private")
                    {                                          
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "hidescript", "document.getElementById('WebPartWPQ4').style.display='none';", true);
                    }
                }
                catch (Exception ex)
                {
                    writer.Write(ex);
                }
            }
        }
    }
    

    Best Regards, G Vijai Kumar | My Sharepoint Blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Wednesday, November 4, 2009 10:24 AM
  • The if statement needs another condition.  It needs to check against the current user and see if its their MySite.  You could do this in a couple of ways:

    1. Check if the user has site collection administrator rights
    2. Load the user profile is the current site, check against the UserProfile.PersonalSite http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.portal.userprofiles.userprofile.personalsite.aspx


    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Wednesday, November 4, 2009 4:42 PM

All replies

  • You can enable personalisation in MOSS http://technet.microsoft.com/en-us/library/cc263073.aspx#section3 .  This would allow users to customise the page and add webparts that will only be shown to them (not shared).

    Dave
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Tuesday, November 3, 2009 5:17 PM
  • How can do this with programming?
    Jamsbond
    Tuesday, November 3, 2009 5:27 PM
  • How do you define private?  What user should see the webpart?  The user who added the webpart?

    A simple way would be to have two webpart properties: 1. for the type (ie private or public), 2. for the user who added the webpart.  Then in the render check the if the webpart is private, if it is then check the current user is the value stored in the second webpart property.  If it fails these validation rules then don't render.  You might have a problem with webpart chrome, but if you use the render method this may be able to suppress the chrome or failing that add some css to hide the webpart.
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Tuesday, November 3, 2009 6:00 PM
  • It is kind of MySite WebPart. each user will have their own data in the web part. If user want to show his data that exists in WebPart, He needs make as public otherwise it will be private.

    Jamsbond
    Tuesday, November 3, 2009 6:03 PM
  • I would still use the same approach just you don't need the second property to store the user name.  You then need to perform the check against the User Profile for the current user and find out if its their mysite.
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Wednesday, November 4, 2009 9:27 AM
  • Hi,

    You can show/hide webpart based on public/private property values with the help of helper webpart, use the below code snippet which has a property where you can set the value as 'Public' or 'Private' in webpart properties area, if you are looking for whenever user clicks on link 'Make Public' you can change the webpart property value to 'Public' by adding a link button and try to change the value in link button on click event


    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Serialization;
    using System.Web.UI.WebControls;
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    
    namespace MSDN
    {
        [Guid("30cfdaad-921d-45ca-ad86-144d11b2a156")]
        public class MSDN : System.Web.UI.WebControls.WebParts.WebPart
        {
            private string _Status = String.Empty;
    
            [WebBrowsable(true),
            Personalizable(PersonalizationScope.User),
            WebDescription("Public/Private"),
            Category("Public/Private"),
            WebDisplayName("Make Public/Private")]
            public string VisibleStatus
            {
                get
                {
                    return _Status;
                }
                set
                {
                    _Status = value;
                }
            }
            
            protected override void Render(HtmlTextWriter writer)
            {          
                SPWeb web = SPControl.GetContextWeb(Context);
                writer.Write(VisibleStatus);
                try
                {
                    if (VisibleStatus == "Private")
                    {                                          
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "hidescript", "document.getElementById('WebPartWPQ4').style.display='none';", true);
                    }
                }
                catch (Exception ex)
                {
                    writer.Write(ex);
                }
            }
        }
    }
    

    Best Regards, G Vijai Kumar | My Sharepoint Blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Wednesday, November 4, 2009 10:24 AM
  • Here is My Question.

    style.display='none';"
    
    This means, You hiding Web part to everyone. But requirement says When user makes private. User can see his web part, but others can not see the web part. If he make Public everybody can see user as well as other users.





    Jamsbond
    Wednesday, November 4, 2009 4:31 PM
  • The if statement needs another condition.  It needs to check against the current user and see if its their MySite.  You could do this in a couple of ways:

    1. Check if the user has site collection administrator rights
    2. Load the user profile is the current site, check against the UserProfile.PersonalSite http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.portal.userprofiles.userprofile.personalsite.aspx


    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Tuesday, November 10, 2009 2:13 AM
    Wednesday, November 4, 2009 4:42 PM