Rob
Rob

Reputation: 1390

Get the GUID of a WebPart in SharePoint 2007

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:

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?

Upvotes: 1

Views: 2937

Answers (2)

Dan
Dan

Reputation: 26

I know this is old, but I came across it querying for something else, and thought it might be helpful to "close it out.". The Microsoft.SharePoint.WebPartPages.WebPart property Rob was looking for (and has probably long ago found) is the StorageKey property.

Upvotes: 1

zincorp
zincorp

Reputation: 3282

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)

Upvotes: 1

Related Questions