locked
Identify used webpart programmatically RRS feed

  • Question

  • Hey, 

    I've got the following situation and I hope somebody here can help me out. I've got a editform (disp- and newform are the same) with a couple of webparts. I'm using the WebPartManager.WebParts to access all available webparts.

    How can I make the connection between the WebPart instance and the webparts available in my Webpart gallery? Which property can be used to identify the underlying webpart?

    The title/displayname of the actual webpart doesn't help since that value could have been changed from it's original value. Thanks in advance!

    Thursday, April 25, 2013 1:37 PM

Answers

  • Hi Jack, 

    either I didn't come through with my problem description or you didn't understand what I'm trying to achieve. I didn't want to create a connection, I tried to find the underlaying webpart from the webpart gallery to all webparts from

    SPWeb web = (SPWeb)properties.Feature.Parent;      
    SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
    
    foreach (var webPart in mgr.WebParts)
    {
       // identify which item from webpart gallery has been used for this webPart
    }

    I think I've found a solution I can work with.

    var myType = webPart.GetType()
    var webPartName = myType.Name + ".webpart"
    
    // I can use webPartName to access the correct item in the webpart gallery.

    Thanks for your help.

    • Marked as answer by Jack-Gao Friday, May 3, 2013 10:09 AM
    Monday, April 29, 2013 7:12 AM

All replies

  • Hi Andreas,

    1. Get the SPLimitedWebPartManager instance on the page;
    2. Get the provider and consumer Web parts need to be connected;
    3. Get the connection points for the provider and consumer Web parts;
    4. Connect them using SPConnectWebParts method. If necessary, certain transforming needs to be in place for compatible interfaces

    SPWeb web = (SPWeb)properties.Feature.Parent;      
    SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);      
    System.Web.UI.WebControls.WebParts.WebPart provider= mgr.WebParts["Provider"];   
    System.Web.UI.WebControls.WebParts.WebPart consumer= mgr.WebParts["Consumer"];  
    ConsumerConnectionPoint addConsumerConnPoint = mgr.GetConsumerConnectionPoints(consumer)["AddressConsumer_ot"];   
    ProviderConnectionPoint addProviderConnPoint = mgr.GetProviderConnectionPoints(provider)["AddressProvider_ot"];      
    mgr.SPConnectWebParts(provider, addProviderConnPoint, consumer, addConsumerConnPoint);

    Thanks,

    Jack


    Jack Gao
    TechNet Community Support

    • Marked as answer by Jack-Gao Friday, May 3, 2013 10:09 AM
    • Unmarked as answer by Andreas Blueher Friday, May 3, 2013 10:55 AM
    Friday, April 26, 2013 6:49 AM
  • Hi Jack, 

    either I didn't come through with my problem description or you didn't understand what I'm trying to achieve. I didn't want to create a connection, I tried to find the underlaying webpart from the webpart gallery to all webparts from

    SPWeb web = (SPWeb)properties.Feature.Parent;      
    SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
    
    foreach (var webPart in mgr.WebParts)
    {
       // identify which item from webpart gallery has been used for this webPart
    }

    I think I've found a solution I can work with.

    var myType = webPart.GetType()
    var webPartName = myType.Name + ".webpart"
    
    // I can use webPartName to access the correct item in the webpart gallery.

    Thanks for your help.

    • Marked as answer by Jack-Gao Friday, May 3, 2013 10:09 AM
    Monday, April 29, 2013 7:12 AM