Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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())
                    {
                        try
                        {
                            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

1 Answer 1

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
1  
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
    
social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/… is what i was looking for :) –  Muhammad Raja Jun 22 '12 at 13:26

Your Answer

 
discard

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.