none
how to get SPContext.current in WCF service ?

    Question

  • Hello,

    I have hosted one WCF service through sharepoint project which works fine if I need to  update any values in database table,

    However I have also written method to update item in sharepoint list.

    But I am getting SPContext.Current as null and unable to get current SPSite object to get desired list object !!!

    Note : I have also tried to accesss SPContext under SPSecurity.RunWithElevatedPrivileges(delegate() code block even SPcontext.Current returns null value.

    Can anyone please let me know how can I access SPContexr.Current in WCF service ?

    Thanks,


    Dipti Chhatrapati

    Monday, June 18, 2012 12:40 PM

Answers

  • Hi,

    Do you have to use SPContext? Can you not just open a new SPSite Object?

    using (SPSite site = new SPSite("http://site"))
    {
    //code here
    }

    You can store the URL to the sharepoint site in a config file?

    Regards


    I find it distasteful to beg for 'Mark as Answer' and 'Mark as helpful'. It's supposed to be about helping people, not about getting the high score.

    Monday, June 18, 2012 1:31 PM
  • SPContext.Current is not available by design, per the link above you can make it available but why do you need it to be available?

    You can get the request url in a wcf service as such,

    Uri requestUri = System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.To;

    Then use that to spin up an SPSite and get the RootWeb, etc as needed.

    Unless you are relying on a third part product that hard coded a reliance to SPContext, or one of the OOTB WebParts like the ReportViewerWebPart (relies on SPContext in it's constructor....) you should be able to get around the need to have an SPContext.Current

    By best practice, you should never use SPContext outside of the context of an actual Http Request for a page on the site.  E.g. Don't use SPContext in web services, or FeatureReceivers.


    My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction

    Monday, June 18, 2012 4:59 PM

All replies

  • Hi,

    Please refer follwoing link in which getting SPContext.current in SharePoint wcf service.
    http://blah.winsmarts.com/2008-9-Getting_SPContextCurrent_in_a_SharePoint_2007_WCF_Service.aspx

    Regards,

    Mukesh Ajmera

    Monday, June 18, 2012 12:59 PM
  • Hi,

    Do you have to use SPContext? Can you not just open a new SPSite Object?

    using (SPSite site = new SPSite("http://site"))
    {
    //code here
    }

    You can store the URL to the sharepoint site in a config file?

    Regards


    I find it distasteful to beg for 'Mark as Answer' and 'Mark as helpful'. It's supposed to be about helping people, not about getting the high score.

    Monday, June 18, 2012 1:31 PM
  • SPContext.Current is not available by design, per the link above you can make it available but why do you need it to be available?

    You can get the request url in a wcf service as such,

    Uri requestUri = System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.To;

    Then use that to spin up an SPSite and get the RootWeb, etc as needed.

    Unless you are relying on a third part product that hard coded a reliance to SPContext, or one of the OOTB WebParts like the ReportViewerWebPart (relies on SPContext in it's constructor....) you should be able to get around the need to have an SPContext.Current

    By best practice, you should never use SPContext outside of the context of an actual Http Request for a page on the site.  E.g. Don't use SPContext in web services, or FeatureReceivers.


    My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction

    Monday, June 18, 2012 4:59 PM