0

I have already found some solution to check if a webpart exist on a page. But I think it is not ok for the performance. It will loop into all webparts on that page. Is there some best practice solution?

    string webPartTitle = "MyCustomWebPart"
        SPLimitedWebPartCollection webparts = spLWM.WebParts;
                                    foreach (System.Web.UI.WebControls.WebParts.WebPart webpart in webparts)
                                    {
                                        if (webpart.Title.Equals(webPartTitle))
                                        {
                                            // webpart found
                                            webPartExist = true;
                                            break;
                                        }
                                    }

UPDATE

I would like also to search by webpart and not by webpart title. It could be possible someone rename the webpart to another name. How can I search by webpart?

By type or something?

  • That is the way to do it. Using LINQ will be equally performance "inefficient". – user2536 Apr 24 '15 at 9:16
  • Would you like a light weight JavaScript to do so? – Asad Refai Apr 24 '15 at 11:48
  • Also are your web parts placed in some page layout or any application page? – Asad Refai Apr 24 '15 at 11:52
  • did you try the gettype() method of webpart class? also check this sharepoint.stackexchange.com/questions/39225/… – Waqas Sarwar MVP Apr 24 '15 at 13:38
  • I've had the same concern about the title and also include the Type part in the check, as added confidence when dealing with custom WP's you usually just have one WP for that type. Title is not even required on a WP. – Cameron Verhelst Apr 24 '15 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.