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 would like to access the information stored inside the toolpart in another webpart. They are defined as follow:

[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable]
[WebDisplayName("Test")]
public bool Test{ get; set; }


Now in the Event Receiver, which is bind to one of the list from the same site, I would like to access above property.
The part about retrieving correct WebPart wasnt so difficult. Then, I thought that properties extraction can be possible by the WebPart.SerializedAttachedPropertiesShared property, however, I was wrong, and this property is not what the name stands for...
Do you have any idea how to access these properties once I have WebPart object? I dont have any clue how to proceed with that...

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
SPSecurity.RunWithElevatedPrivileges(delegate()
{
   // get the site in this context
   using (SPSite SiteCollection = new SPSite(mySiteGuid))
   {

       // get the web in this context
       SPWeb myWeb = SiteCollection.OpenWeb(myWebGuid);
       myWeb .AllowUnsafeUpdates = true;

       // Hide/display another webpart.
       Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = null;

       mgr = myWeb.GetLimitedWebPartManager("default.aspx",System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

      foreach (System.Web.UI.WebControls.WebParts.WebPart myWebPart in mgr.WebParts)
      {
           // If this is the webpart we want to modify...
           if(myWebPart.Title == "Other Webpart Name")
           {
                // Toggle the visibility state.
                myWebPart.Visible = !myWebPart.Visible;
                mgr.SaveChanges(myWebPart);
                break;
           }
      }
   }
});

http://social.msdn.microsoft.com/forums/en-US/sharepointdevelopment/thread/7f12b692-dbbb-44d3-90fe-aff0a13213c0/

code above is just looping through the webparts within the webpart manager, as your using the foreach WebPart means the varible (object) myWebPart is a webpart, you then check to make sure its the webpart you want with an if statment, within the if you can do what you want to the webpart property!

for another example you can use this code but its using the webpart ID instead!

http://jeremyknight.wordpress.com/2010/11/12/programmatically-update-web-part-properties/

now your asking well i can already do that or it doesnt give me access still to the property!? well thats because you need to setup the webpart in question properly to show its properties ;) follow my guide here:

Question about webpart properties

call the public accessor e.g. myWebPart.MyString

There are other methods that would also save alot of time and alot of code! e.g

when you build the webpart that you want to ammend (its properties) you can go to build folder and add that DLL into the webpart that will be changing the properties and than you have direct access to properties ;)!

This is a method that i would use as its far less code! no looping though webpart manager ect! no fuss!

use this as a guide if you dont know how!

http://blogs.thesitedoctor.co.uk/tim/2007/02/13/ImportingReferencing+DLLs+In+Visual+Studio.aspx

dont forget to also add the dll into the GAC (assembly folder)

1) get the webpart you want to ammend (dll)

2) open the new webpart that will be changing the webpart above

3) add reference to that dll (follow guide in link above)

4) code, use the methods class ect ;)

5) build webpart add to sharepoint ect

6) add dll from first webpart into GAC

share|improve this answer
add comment

Thanks for insights:) Importing the dll is the best solution, now I am able to retrieve everything what I want:)

Type webPartType = typeof(WebPart.VisualWebPart1.VisualWebPart1);
using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
            {

                foreach (WebPart wp in wpm.WebParts)
                {
                    if (wp.GetType().Equals(webPartType))
                    {
                        webPart = (WebPart.VisualWebPart1.VisualWebPart1)wp;
                        break;
                    }

                }
            }
share|improve this answer
add comment

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.