Home > MOSS 2007, SharePoint, SharePoint 2010 > Be very, very careful when you use SPSite.OpenWeb()!

Be very, very careful when you use SPSite.OpenWeb()!

I recently came across what can only be described as a shocking and dangerous bug in the SharePoint API.

According to the MSDN documentation the SPSite.OpenWeb() method:

“Returns the site that is associated with the URL that is used in an SPSite constructor.”

Below is an example of how it is very commonly used:

using (SPSite site = new SPSite("http://www.myserver.com/parentSite/childSite"))
using (SPWeb web = site.OpenWeb())
//Do your stuff

Going by the MSDN documentation the code above should return the web site located at http://www.myserver.com/parentSite/childSite. This is further confirmed by the following statement:

“When used in conjunction with an SPSite constructor, the OpenWeb method returns the lowest-level site specified by the URL that is passed as parameter for the constructor.”

This is all true if the site located at the specified url exists. But what if it doesnt exist?

Consider a scenario where an SPWeb with the url http://www.myserver.com/parentSite exists but an SPWeb with the url http://www.myserver.com/parentSite/childSite does not exist. In this scenario you would expect the code above to throw an exception, however, it does no such thing instead it ends up opening a completely different SPWeb. In this specific scenario it returns an SPWeb with the url http://www.myserver.com/parentSite. If an SPWeb didnt exist at this url as well then it would have returned the RootWeb! (i.e. http://www.myserver.com if it existed)

In other words you would end up opening and performing actions on a totally different SPWeb! As you can imagine this can have very dangerous consequences as I only very recently found!

  1. February 28, 2013 at 10:14 am

    This book can aid even the least-knowledgeable person to get the best
    six pack abs quick, because of this book’s fool-proof good quality. With a diet plan low in fat, you are going to use up the fat shops about your midsection, revealing your ab muscle tissues.

  2. February 28, 2013 at 10:25 am

    Pellicer, youll uncover that it could literally alter your life.
    But confidence is some element distinct.He goes a lot extra than a lot of tiny,
    and commonly unnoticed elements, and how they generally
    be crushing to how attracted women is constantly to you.That implies in
    reality exuding these precise very same feelings as well as
    the precise similar aura that enables those pals to pick up girls.
    But altering your self for the much better, so it
    is doable to promote actual life outcomes with the way which you are in a position to upgrade your life for
    the far better.

  3. February 28, 2013 at 11:10 am

    Then, it is crucial to sculpt them.

  4. February 28, 2013 at 11:11 am

    Well-known choices incorporate mayonnaise, olive oil, and
    vinegar lice treatment options. Its aim is to present you an effective head lice treatment
    remedy that will enable you to get no cost from all types of head lice nightmare.

  5. March 17, 2013 at 2:01 pm

    Different testimonials make it hard to make a decision its credibility.
    There are no pills or supplements of any sort that will assist you drop your abdominal
    fat more rapidly.If you are still in a confusion of
    whether or not it operates or not, the greatest way to uncover out is to attempt it and be
    the best judge.The plan has effectively worked for
    more than 270,000 people and there are many testimonials.The workouts had been developed to perform
    you tricky but as effectively as doable.You then have 21 days to make a decision if
    you feel the data is worth your challenging-earned dollars and choose to pay the rest
    of the cost.So you can attempt it 1st and make a decision to purchase
    later, instead of paying in complete up
    front.So is it worth it?He utilizes one of a kind, unconventional procedures to
    build an productive formula for reaching low levels of physique fat speedily.
    While most of the workouts are concentrated on functioning your entire body, he does recommends 5
    minutes of actual direct abdominal perform two-3 occasions a week.
    Ask oneself this, do you want to drop weight?
    You just require some weights and an exercise ball to do these workout routines in a comfortable style.

    Plus as a bonus you can make strong ab muscle to show off.
    Mike is a major supporter of consuming 5-6 modest intake a day that are comparable in size.

  6. May 8, 2015 at 4:43 pm

    Is this problem still existed in SharePoint 2010 whenever using SPSite.OpenWeb() ?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: