SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am adding three webparts with different titles to a given page url, but I need to check if webpart is already on page or not, here is the code i am using atm to add webpart to the page.

     using (SPSite mySite = new SPSite(PageUrl))
                    using (SPWeb web = mySite.OpenWeb())
                            web.AllowUnsafeUpdates = true;

                            using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
                            webpart webpart = new webpart();
                            webpart.AllowClose = true;
                            webpart.AllowMinimize = true;
                            webpart.AllowConnect = true;
                            webpart.ChromeType = PartChromeType.None;
                            webpart.ChromeState = PartChromeState.Normal;
                            webpart.AllowZoneChange = true;
                            webpart.AllowHide = true;
                            webpart.ExportMode = WebPartExportMode.All;
                            webpart.Hidden = false;
                            webpart.AllowEdit = true;
                            webpart.AllowZoneChange = true;
                            webpart.AllowHide = true;
                            webpart.ButtonCSSClass = "DefaultButton";

                            manager.AddWebPart(webpart, "Right", index);
share|improve this question
up vote 4 down vote accepted

Here is LINQ code that finds a Web Part with a title:

    string _webPartTitle = "mywebpart";
    //Retrive the webpart with some title
    IList<Microsoft.SharePoint.WebPartPages.WebPart> _listFormWebParts = (from _wp in manager.WebParts.Cast<Microsoft.SharePoint.WebPartPages.WebPart>()
                                                                            where string.Compare( _wp.Title , _webPartTitle  , true ) == 0
                                                                            select _wp).ToList();

    //Check if there are any web parts found
    if (_listFormWebParts != null)

share|improve this answer
getting error on 'Microsoft.SharePoint.WebPartPages.SPLimitedWebPartCollection' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'Microsoft.SharePoint.WebPartPages.SPLimitedWebPartCollection' could be found (are you missing a using directive or an assembly reference?) – Muhammad Raja Jun 22 '12 at 13:06
Add a reference ´using System.Linq;´ – Falak Mahmood Jun 22 '12 at 13:10
sorry forgot to mention, I am using .net 2.0 :/ – Muhammad Raja Jun 22 '12 at 13:14
Can i use "SPLimitedWebPartManager" to get webparts of a page and then match there title or webpart object.. – Muhammad Raja Jun 22 '12 at 13:23… is what i was looking for :) – Muhammad Raja Jun 22 '12 at 13:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.