scientiasro
asked on
SPContext.Current.Web returns nothing (NullReferenceException)
Dear experts.
I am trying to write a workflow in C# in VS 2013 for Sharepoint 2010. I never did it before nor am I a programmer, so I need to kindly ask for a bigger explanation.
The workflow is running each time the new item is created in the list. I am trying to retrieve an item from some other list which exists in the same site. The web page I am following is following:
http://msdn.microsoft.com/ en-us/libr ary/office /ms456030( v=office.1 4).aspx
It is pretty clear from the referenced MSDN site, how you retrieve such items from lists. However because I am missing some really basic programming knowledge I stopped at the first most important line:
Please don't hesitate to ask if you need more information. I don't know what information is helpful for solving my issue.
I am trying to write a workflow in C# in VS 2013 for Sharepoint 2010. I never did it before nor am I a programmer, so I need to kindly ask for a bigger explanation.
The workflow is running each time the new item is created in the list. I am trying to retrieve an item from some other list which exists in the same site. The web page I am following is following:
http://msdn.microsoft.com/
It is pretty clear from the referenced MSDN site, how you retrieve such items from lists. However because I am missing some really basic programming knowledge I stopped at the first most important line:
SPWeb web = SPContext.Current.Web;
While debugging NullReferenceException has been thrown. How am I able to use aforementioned statement successfully, so the object web won't be empty (of value null)? Why is the object web empty?Please don't hesitate to ask if you need more information. I don't know what information is helpful for solving my issue.
ASKER
Hello Ramkisan.
I don't know what is helpful. Therefore I encouraged everybody to ask. So your question would be "where did I try to get SPcontext".
I tried to get it from within the workflow, so I guess you are right and it will be null because of the reason it is different thread. Could you please post an example how can I use it within a code in respect to msdn site I've posted, so I am able to use the code they suggest for retrieving items from a list? It would really help me because I am not a programmer.
I've tried this without success:
eb' does not contain a constructor that takes 1 arguments"
Thank you.
Michal
I don't know what is helpful. Therefore I encouraged everybody to ask. So your question would be "where did I try to get SPcontext".
I tried to get it from within the workflow, so I guess you are right and it will be null because of the reason it is different thread. Could you please post an example how can I use it within a code in respect to msdn site I've posted, so I am able to use the code they suggest for retrieving items from a list? It would really help me because I am not a programmer.
I've tried this without success:
WorkflowContext wflowcont = new WorkflowContext();
string weburl = wflowcont.CurrentWebUrl;
SPWeb currentWeb =new SPWeb(weburl);
Visual Studio says "'Microsoft.Sharepoint.SPWThank you.
Michal
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
My solution is more complete.
if it is coming null with
SPWeb currentWeb = SPContext.Current.Web;
Please try to get it with
SPWeb currentWeb =new SPWeb("your web URL");
If you are trying to get it in the workflow code then it will definitely be null as workflow runs in different thread to current context. In this case you can use WorkflowContext.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.workflowactions.workflowcontext_properties.aspx
Or you can get the current web as stated above with using web URL.
Hope this will help you.