1

newdocset doesnot seems to work with SPContext.Current.Web I have a HttpModule below is a sample code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web;

namespace mynamespace
{
    class DemoHttpModule : System.Web.IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(System.Web.HttpApplication application)
        {
            application.BeginRequest += new EventHandler(Application_BeginRequest);
            application.AcquireRequestState += new EventHandler(this.Application_AcquireRequestState);
            application.ReleaseRequestState += new EventHandler(this.Application_ReleaseRequestState);
            //To Handel page.PreRender
            application.PreRequestHandlerExecute += new EventHandler(this.Application_PreRequestHandlerExecute);
        }
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication curApp = sender as HttpApplication;
            //SPWeb oWeb = SPContext.Current.Web;
        }
        private void Application_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication curApp = sender as HttpApplication;
            if (curApp.Request.Url.ToString().Contains("advsetng.aspx"))
            {
                return;
            }

            using (SPSite oSPsite = new SPSite(Convert.ToString(curApp.Request.Url)))
            {
                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                {

                }
            }
        }
        private void Application_ReleaseRequestState(object sender, EventArgs e)
        {
            HttpApplication curApp = sender as HttpApplication;
            SPWeb oWeb = SPContext.Current.Web;
        }
        private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication curApp = sender as HttpApplication;
            SPWeb oWeb = SPContext.Current.Web;
        }
    }
}

in Application_AcquireRequestState if i am using

using (SPSite oSPsite = new SPSite(SPContext.Current.Web.ID)
            {

Then although it is not generating any exception but shows an error message while creating a newdocset stating "This action cannot be completed"

if i am using

using (SPSite oSPsite = new SPSite(Convert.ToString(curApp.Request.Url)))
                {

then it is working fine, but the problem with curApp.Request is it will always return the root level web object So are there any alternatives to SPContext.Current.Web.ID

in this link the answer suggested by spunkyvt tells something about a similar problem but i cant use the solution suggested by him simillar problem link

Any suggestions

Thanks Mac

0

First of all, you are trying to create and SPSite with the ID of an SPWeb. That's is never going to work. Try the following instead.

using (SPSite oSPsite = new SPSite(SPContext.Current.Site.ID))
{

}

Out of curiosity, why are you using an HttpModule? Can't you do it with an invisible custom control injected in the masterpage using the AdditionalPageHead DelegateControl? That way you simplify things a lot more and you should have access to all the context you need from within your control.

I only write custom HttpModules if I can't think of any other solution that works.

8
  • lots of things are going on through this httpmodule i have just giiven a sample code. otherwise it will mess up the whole things let me see if this works
    – user4205
    Jan 31 '12 at 14:59
  • Nope i am getting the same error "The action cannot be completed"
    – user4205
    Jan 31 '12 at 15:06
  • Attach debugger and confirm SPContext.Current.Site.ID contains a Guid. Jan 31 '12 at 15:57
  • ya it does as i mentioned code is generating no exception and returns everything as expected but at the end this error comes commenting the SPContext line everythings work as desired, but i need the current web object
    – user4205
    Jan 31 '12 at 16:12
  • Why are you instantiating a new SPWeb if you already have one in the Context? why not use SPContext.Current.Web itself? Jan 31 '12 at 22:46
0

Fixes the issue with the following code

    if (Convert.ToString(curApp.Request.Url).ToLower().Contains("newdocset.aspx"))
                    {
                        using (SPSite oSPsite = new SPSite(curApp.Context.Request.Url.ToString()))
                        {
                            using (SPWeb oSPWeb = oSPsite.OpenWeb())
                            {
// doing my stuff here
    }
    }
    }
1
  • ThisSPContext problem with new docset happens only in AcquireState and obviously in BeginRequest
    – user4205
    Feb 2 '12 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.