views:

311

answers:

1

I'm trying to emit some jQuery and other Javascript that will hide and show WebParts on a page. What I'd like to do is find one of two things:

  • The ID of the table cell that contains the WebPart (i.e. MSOZoneCell_WebPartWPQ5)
  • The WebPartID of the div tab of the WebPart that shows in the HTML (i.e. WebPartID="059611a7-adef-479e-bda9-fe5799dc62d1")

I've looked at the WebParts in the Zone I want to impact using the following code:

        System.Web.UI.WebControls.WebParts.WebPartZoneBase
        myZone = this.Zone;

        if (myZone != null)
        {

            for (int i = 0; i < myZone.WebParts.Count; i++)
            {
                // Get the web part
                System.Web.UI.WebControls.WebParts.WebPart wp =
                    myZone.WebParts[i] as System.Web.UI.WebControls.WebParts.WebPart;
                if (wp != null)
                {
                    // Build an XPath query to get the attribute for
                    // this web part
                    string xpathQuery = "/tabs/tab[@name='" + wp.Title + "']";

                    XmlElement wpElement =
                        tabConfigDoc.SelectSingleNode(xpathQuery) as XmlElement;

                    if (wpElement != null)
                    {                            
                        hideTabsJS.AppendFormat("$(\"#{0}\").hide(); ", wp.ID);
                        //switchTabsJS.AppendFormat("$(\"#{0}\").hide(); ", wp.ClientID);
                    }
                }
            }

The problem is that none of the APIs for the WebPart or WebPartManager seem to provide this information. Is it possible to derive one of the two IDs?

+1  A: 

Just of curiosity, have you tried casting the web parts as Microsoft.SharePoint.WebPartPages.WebPart and accessing the ID from that?

(from http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.webpart.id.aspx)

zincorp
Yes, the ID property comes back something like "g_059611a7_adef_479e_bda9_fe5799dc62d1". Am I stuck just doing a String.Replace to get rid of the g_ and then another to replace the underscores with dashes? Or is there a more elegant way to do this?
Rob
If there are no properties exposed otherwise to retrieve this value then it may be the only way to do this. It isn't necessarily *too* lacking in elegance
zincorp