iPhone Detected, site running in minimal mode.
Home     Tags/Archives     Tweets     About Kevin

So sometimes you need to pop in some code, lets say for example in a web part.. that needs to do something that the user running the webpart doesn't normally have permission to do.  There are a couple of solutions possible all relating to various forms of impersonation.
 
WSS3 adds an extremely easy way (albeit, not necessarily the most secure way, but let's pretend that we trust our developers implicitly, and this DLL is going into the GAC).
 
SPSecurity.RunWithElevatedPrivileges(delegate codeToRun)
 
Ok super.  But a big gotcha is that if you try and pull SPContext.Current.Web from within the delegate it will return an SPWeb object in the context of the current user, not one with elevated privileges (sort of makes sense if you think about it).  Unfortunately in a webpart scenario the context object is pretty key, so here's one work around (I'm sure there are others--- let me know).
 
//Get data to recreate "context" later with privileges
SPWeb webUserContext = SPContext.Current.Web;
Guid webGuid = webUserContext.ID;
Guid siteGuid = webUserContext.Site.ID;

// run as system administrator
SPSecurity.RunWithElevatedPrivileges(delegate()
{
     // get the site in this context
    
using (SPSite site = new SPSite(siteGuid))
     {
         
// get the web in this context
         
SPWeb web = site.OpenWeb(webGuid);

          // do you stuff here
      }
});


Having returned from TechEd last week (and recovered this week) I wanted to share with everyone some general themes from the conference.  I'll probably be incorporating many of the session specific things I learned in upcoming Ambassador University sessions, so I'll just focus on the big picture stuff here, and specifically the keynote.
 
A big theme of the conference was on "Agility".  Many of us groaned at the use of this buzzword because it just confuses the definition even more, but it certainly made many appearances throughout, including the Keynote.  The context this was put in was a sort of maturity model which goes something like this: COST-QUALITY-AGILITY.  This maps pretty close to what Gartner and Microsoft have been talking about in the Infrastructure Optimization models this past year (CoreIO, BPIO, and AppIO).  It encompasses just about every MS initiative from their virtualization strategy to people ready marketing to agile development.
 
Another theme was "we've got the stuff here and now, vision-speak begone".  This was manifested in a cutesy Back-to-the-Future movie at the start of the keynote, culminating with an onstage appearance by Christopher Lloyd and a Delorian.  Again, this relates to the first theme in that MS of course has visions, but they've been burned in the past by making them public because in today's world, technology changes so rapidly that you end up changing your visions pretty quickly (to keep your business agile).
 
If you want to watch the whole keynote yourself you can find it here:
 
Here are some timings if you are interested in only particular sections:
00:00 Back-to-the-Future Vision Movie
10:30 IT Challenges/Infrastructure Optimization
17:50 Energizer Batteries Case Study Video
21:00 Tom Bittman, Gartner - Dynamic/Agility
33:15 Areas of Focus
39:10 Virtualization
40:30 Server 2008 and SCVMM Demo
49:28 Processes and Models
53:30 SCOM Demo
60:01 Service-Enabled App Platforms
63:40 BizTalk 2008 / SQL 2008 Reporting Services Demo
70:25 User Focused Software
71:55 OBA and VS 2008 Demo
79:30 Silverlight Demo
87:00 Product Releases and Roadmaps
89:50 TechEd 15th Aniversary Video


If you need to stash some information for later use (for example, by a web part) in a SharePoint (MOSS or WSS) site a great place to do it is the SPWeb PropertyBag.  However, I recently ran into some quirks with this so I thought I'd share my lessons learned:
 
The PropertyBag object is accessed via the .Properties property of an SPWeb object and it's a StringDictionary.  However, know that this property is readonly (the property itself, not the PropertyBag object).  This means that to make any changes to it you need to first get a reference pointer to it.  Put another way, this code will not work:
 
SPWeb web = site.RootWeb;
web.Properties["SomeProp"]="SomeValue";
web.Properties["SomeProp"].Update();
 
However, this code will:
 
SPWeb web = site.RootWeb;
SPPropertyBag prop = web.Properties;
prop["SomeProp"]="SomeValue";
prop["SomeProp"].Update()
 
Also, don't forget to call .Update() or it won't persist to the database.  When you are just reading data out, you don't need to worry about the above subtlety of getting a reference first.
 
There isn't a PropertyBag on the SPSite object, but don't worry... if you need to stash data at the site collection level, just use the RootWeb object (as I did in the above example).  You may want to preface the StringDictionary key with s_ or site_ in these cases to avoid collisions with data intended to be "virtually" scoped at the web level.
 
Where are some places it may make sense to store data here?
  • When activating a feature, use a FeatureReceiver class to populate needed inital data.
  • Update the data via a web part or a _layouts application page (don't forget to write a feature to add a link somewhere to the app page).
  • Consume this data from webparts, app pages, workflows, list event handlers, etc...

Obviously you can also store this type of data in a list, however, often this is not desired because a) requires more overhead in the actual reads/writes, b) can be more easily messed up by users, c) is messier, and d) is a bit overkill if you just need a simple key/value string pair or a few of them.  Of course if you need dimmensional flexibility, then the list is better since you can add columns and can store multiple values (rows).

 










RSS FeedBack to the HomepageMy Twitter FeedMy Stumbles

Tags

Hide Low Frequency Tags

Archives

Recent Posts