locked
Showing/Hiding web parts RRS feed

  • Question

  • Hi,

    I am trying to show or hide web parts based on a set of rules. I'm not sure which programming language to use, whether I even need to use one (or if I can accomplish this through sharepoint), or where to put the code if I do need to use a programming language. I do however know how to program in general but not in tandem with sharepoint. My rules are similar to (show web part 2 if web part 1 has content equal to X). Please tell me what the best course of action to accomplish dynamically showing/hiding web parts would be.

     

    I have looked into IRuntimeFilter and LimitedWebPart but I'm not sure if either of these is a good idea. Just looking for a little direction. Thanks.

     

    Wednesday, September 22, 2010 3:16 PM

Answers

  • Hi,

    I am not sure that the suggestion above  from Sandip works for your issue.

    But You can try to create a logic web part used to control the display of one web part while the web part is hiddened to do all you need. 

    You can get one web part with the following code.

    e.g

    this.Page.FindControl("webpart name")
    
    Then you can decide whether it is hiddende by assigning bool value of the property Hidden of the web part.
    Microsoft Online Community Support
    • Marked as answer by Porter Wang Tuesday, September 28, 2010 8:37 AM
    Thursday, September 23, 2010 6:51 AM
  • Hi there,

    I don't think Visibility Filter can entirely resolve this either. Thanks KeFang for sharing your thoughts. However please consider this suggestion as well.

    You can achieve your requirement if you cast one(main action related) webpart as a custom webpart. Lets say you build custom webpart 1 with drop down list and on its content change you want to hide your another web part 2.

    The event (not sure you want button click or drop down list or whatever, you have not mentioned) method you can use WebPartManager to hide the web part 2. Please look at the code block. Sorry the code is in VB..

     ' Web Part Manager Object
                Dim mgr As Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager = Nothing
                mgr = portfolioWeb.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)

                For Each oWebPart As System.Web.UI.WebControls.WebParts.WebPart In mgr.WebParts
                If oWebPart.Title = "thewebparttitleuwanttohide"
                    oWebPart.Hidden = True
                End If
            Next

    And that is it, this will hide your webpart 2 based on the action prompted from webpart 1 (Provided you have a custom webpart 1 ) however if you don't have custom webpart then you might want to investigate converting both into dataview webparts and using XSLT capabilities of SPD hook them together to show / hide contents.

    Hope this helps you,

    Good luck

    Thanks

    • Marked as answer by Porter Wang Tuesday, September 28, 2010 8:37 AM
    Thursday, September 23, 2010 8:38 AM

All replies

  • Have u tried this? SharePoint Web Part Visibility Filter Web Part
    Regards, Sandip Patil, Sharepoint Developer www.worldofsharepoint.com
    Wednesday, September 22, 2010 3:39 PM
  • Hi,

    I am not sure that the suggestion above  from Sandip works for your issue.

    But You can try to create a logic web part used to control the display of one web part while the web part is hiddened to do all you need. 

    You can get one web part with the following code.

    e.g

    this.Page.FindControl("webpart name")
    
    Then you can decide whether it is hiddende by assigning bool value of the property Hidden of the web part.
    Microsoft Online Community Support
    • Marked as answer by Porter Wang Tuesday, September 28, 2010 8:37 AM
    Thursday, September 23, 2010 6:51 AM
  • Hi there,

    I don't think Visibility Filter can entirely resolve this either. Thanks KeFang for sharing your thoughts. However please consider this suggestion as well.

    You can achieve your requirement if you cast one(main action related) webpart as a custom webpart. Lets say you build custom webpart 1 with drop down list and on its content change you want to hide your another web part 2.

    The event (not sure you want button click or drop down list or whatever, you have not mentioned) method you can use WebPartManager to hide the web part 2. Please look at the code block. Sorry the code is in VB..

     ' Web Part Manager Object
                Dim mgr As Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager = Nothing
                mgr = portfolioWeb.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)

                For Each oWebPart As System.Web.UI.WebControls.WebParts.WebPart In mgr.WebParts
                If oWebPart.Title = "thewebparttitleuwanttohide"
                    oWebPart.Hidden = True
                End If
            Next

    And that is it, this will hide your webpart 2 based on the action prompted from webpart 1 (Provided you have a custom webpart 1 ) however if you don't have custom webpart then you might want to investigate converting both into dataview webparts and using XSLT capabilities of SPD hook them together to show / hide contents.

    Hope this helps you,

    Good luck

    Thanks

    • Marked as answer by Porter Wang Tuesday, September 28, 2010 8:37 AM
    Thursday, September 23, 2010 8:38 AM
  • Where would that VB code go? .. Should it be embedded in the aspx page? Would it be developed in visual studio? I tried that with C# code but it gave an error message that scripts were prohibited. I'd be willing to learn VB but first I need to make sure it is the best option. Also, is it possible to start out with SP's default web part (lets say the data list web part), inherit that web part, and then implement some custom behavior into it? If so, how would I do that? Where would I put the code? What language should I learn?

     

    Thanks. For now I'll investigate the XSLT (I'm assuming you meant XSL? - like in the "modify web parts -> XSL editor"?)

     

    Sorry if that is a lot of questions.

     

     

    Thursday, September 23, 2010 1:53 PM
  • Hi there,

    Please no need to say sorry, like you I am also learning Sharepoint and we both are in the same boat. Thanks for getting back on this.

    Let me answer your questions like you asked them

    Where would that vb code go ?

    This code will go inside a custom webpart that you will have to create and deploy on your sharepoint environment. Your deployment will follow this article which will use Visual Studio 2005,08,2010 with Sharepoint extensions installed giving you template of webpart and many others.

    Look at this article to see how a basic webpart is being created. [ http://www.codeproject.com/KB/sharepoint/SharepointContactForm.aspx ] *

    I'd willing to learn vb

    There is no need to do so if your preference is C# in order to switch between vb to c# and vice versa use this tool and make it a bookmark, can be useful for getting code converted. [ http://www.developerfusion.com/tools/convert/csharp-to-vb/ ]

    Also, is it possible to start out with SP's default web part (lets say the data list web part), inherit that web part

    I am afraid, you cannot do this. I wish I could do the same by overriding the list view webpart customize them the way I like but it doesnot work like that. You can customize LVWP to a DVWP (data view) webpart by opening the page in Sharepoint Designer and coverting it to XSLT which is what I mention and you are then exposed to lots of options you can do with this webpart like customizing it. Read this. [ http://www.sharepoint-tips.com/2007/05/sharepoint-designer-article-2-creating.html ]

    What language should I learn

    I won't tell you what to learn because it is all a matter of where you are comfortable at. I use vb simply because I have used it always and I sometimes use C#. However, the common industry standard in C# and easier to get jobs anywhere having C# understanding. So you are free to decide for yourself.

    Finally, don't get daunted by all this. Take your time and learn it all and most of all have fun. Learn something and experiment and then you will find it easy to implement your requirements. In the meantime please post your questions back to the forum here so that MVP's and experts can reply to you.

    Remember, your List View Webpart is simply a view of list but you can use custom webpart (like  * link ) to output contents of the list using Sharepoint Object Model and capture user events and then hide / show some other webpart.

    Hope that helps,

    Good luck

    Thanks

     

    Thursday, September 23, 2010 2:37 PM
  • Thanks for that extremely detailed reply. I'm also looking into modifying the xsl in order to cause a web part to not render, is that possible?
    Thursday, September 23, 2010 4:46 PM
  • Hi,

    If you would like to use the xsl to control the display of the webpart, how do you get the content of one webpart by using xsl ?

    I don't think it's possible to get it.


    Microsoft Online Community Support
    Friday, September 24, 2010 2:11 AM
  • Hi,

    KeFang suggests right. Unfortunately there are not enough properties when you convert a list view web part to XSLT data view webpart and hooking into other webpart is simply out of the question. Your best bet is to create one custom webpart and based upon its events show / hide other webpart accordingly.

    Hope you resolve your requirements.

    Good luck

    Thanks

    Friday, September 24, 2010 9:04 AM
  • Thanks a lot for all of your help guys. I think I've figured out how to query the DB from XSL; however, is there a way to tell the webpart to hide from within its XSL? I noticed that the XML has a property called Hidden and if you could somehow set hidden to "true", it should hide, but can this be done from XSL?
    Friday, September 24, 2010 1:15 PM