When I created sharepoint webpart and Used


using (SPSite site = new SPSite("http://Server:port/sites/Site/"))
{
using (SPWeb web = site.OpenWeb())
{
//Some Code Here.
}
}

It worked fine but before deploying I changed new SPSite(“http://Server:port/sites/Site/”) to SPContext.Current.Site and it started throwing the above exception on the Error Page but while debugging there was no Exception.

Weird issue and I don’t have any explaination for it but what helpd me solving the issue was the fact when we are passing Url to the SPSite it works fine.

So on the same lines I used SPContext.Current.Web.Url to fetch the Url of current siteand It worked like a charm.

So instead of above if we use


using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
//Some Code Here.
}
}

it works. 🙂

Leave a comment

I’m Abhinav

Welcome to my Blog. I am a passionate architect and developer dedicated to creating innovative and user-friendly digital experiences. With a background in both design and development, I bring a unique blend of creativity and technical expertise to every project I undertake.

In addition to my technical skills, I have a keen interest in the latest industry trends and enjoy sharing my knowledge through blogging and speaking engagements. When I’m not coding or designing, you can find me exploring new technologies, reading about the latest in tech, or indulging in my love for music and travel.

Let’s connect