Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

SPContext - object reference not set to an instance of an object

  Asked By: Asksuresh    Date: Jun 15    Category: Sharepoint    Views: 6365

When I try to use SPContext in my VS2008 code I get the "object reference not
set to an instance of an object" error. I have Microsoft. SharePoint referenced
as well. Is there something I need to do to reference SPContext objects?



19 Answers Found

Answer #1    Answered By: Dameon Dejesus     Answered On: Jun 15

SPContext is only available when you are running code  in the context of an
HTTPRequest on a sharepoint  site. Are you by any chance trying to use it in
an Event handler, a workflow, or a console application? If so, then you
can't and need to get a reference  to the web or site you want in a different

Answer #2    Answered By: Gladys Trevino     Answered On: Jun 15

Yes, inside an SPDÂ workflow that calls a custom action. The custom action calls

You're saying I can't do that?

Answer #3    Answered By: Stacey Bryant     Answered On: Jun 15

Workflows, SPD or otherwise, don’t run in the context of an HTTP request.
They run in the backend process of the server. As such they have no SPContext
object available to them.

What are you using SPContext.Current to retrieve? The site collection, the web
or something else?

Answer #4    Answered By: Mckenzie Mcknight     Answered On: Jun 15

You don't need SPContext for that; it's already accessible to the workflow.
Via SPD, it's called "Current Item"; on the SPWorkflow class, I believe
you'll want the ParentItem property.

Ah, here it is:


Answer #5    Answered By: Marion Head     Answered On: Jun 15

I've been looking in the SDK and searching all over on how to use the
SPWorkflow. The SDK isn't much help with this.

I'm thinking something like:

SPListItem myItem = SPWorkflow.ParentItem;

But that's not working at all.

Answer #6    Answered By: Felipe Osborne     Answered On: Jun 15

Are you getting an object  back with the ParentItem property? Is it the
wrong item? Is it throwing an error?

What about the ItemId property? That's supposed to give you the ID field of
the current item.

Answer #7    Answered By: Wilbert Justice     Answered On: Jun 15

I'm not using ParentItem because I can't figure out the syntax. When I type
SPWorkflow, ParentItem is not available.

So I should be able to use either SPWorkflow.ParentItem or SPWorkflow.ItemID?

Answer #8    Answered By: Maricela Conway     Answered On: Jun 15

I think what you are looking for is this:

SPListItem myItem = new SPListItem(SPWorkflow.ItemGuid);

The ItemGuid property of the workflow is the GUID of the ListItem that started
this instance  of the workflow. Once you have the GUID you can use the new
constructor to get a copy of the actual listitem.

Answer #9    Answered By: Ty Wilkerson     Answered On: Jun 15

On a positive not I have been able to output some variables to the workflow
history log to get some sort of debugging going. Not ideal, but better than

Answer #10    Answered By: Zachery Cross     Answered On: Jun 15

SPWorkflow.ItemGuid is apparently not available.

Answer #11    Answered By: Latanya Nieves     Answered On: Jun 15

Are you doing this with the current workflow instance, or trying to call the
properties on the general SPWorkflow class?

Answer #12    Answered By: Jimmie Morton     Answered On: Jun 15

The actual code  would be something more like this.ItemGuid where this
represents the current running workflow. It would need to be a static method to
be callable directly from the SPWorkflow class. That’s what SPContext
essentially is.

Answer #13    Answered By: Jimmy Abp     Answered On: Jun 15

Due to dealine constraints I have decided to do this all manually in SPD. But
did figure out how to access the .xoml file and copy the separate actions that I
must do on each field. So now all I have to do is create a step for each field I
need to check. In each step I do an If and Else condition. Then I can copy the 9
interim steps from one to the other! That will save a lot of time. I should blog
the solution because I didn't it anywhere out there. But then again I would need
a SP blog wouldn't I?

Answer #14    Answered By: Jabari Burks     Answered On: Jun 15

public SPWorkflowActivationProperties workflowProperties = new

Guid guid = this.workflowProperties.Item.ID;

Answer #15    Answered By: Ismael Todd     Answered On: Jun 15

I have decided to do this all in
SPD. But I did figure out how to copy the actions in the .xoml file so that I
didn't have to repeat all those steps!

Answer #16    Answered By: Uriel Valencia     Answered On: Jun 15

SPContext.Current doesn't work????????????

Answer #17    Answered By: Jody Holloway     Answered On: Jun 15

No. Anytime I use it I get the object  not set  error. I am using it in VS2008 on
a server with MOSS2007.

Answer #18    Answered By: Lyndsey Gibbs     Answered On: Jun 15

what context are you running this?????????