Solved

SPContext.Current.Web returns nothing (NullReferenceException)

Posted on 2014-09-11
5
1,560 Views
Last Modified: 2014-09-17
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/library/office/ms456030(v=office.14).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:
SPWeb web = SPContext.Current.Web;

Open in new window

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.
0
Comment
Question by:scientiasro
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40318529
I am not sure where you are trying to get your SPcontext,
 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.
0
 

Author Comment

by:scientiasro
ID: 40318836
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:
WorkflowContext wflowcont = new WorkflowContext();
string weburl = wflowcont.CurrentWebUrl;
SPWeb currentWeb =new SPWeb(weburl);

Open in new window

Visual Studio says "'Microsoft.Sharepoint.SPWeb' does not contain a constructor that takes 1 arguments"

Thank you.
Michal
0
 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 500 total points
ID: 40318874
Hi
Do you have work flow properties  in your code?

Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties WorkflowProperties =new Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();

if yes, you can access all the properties you want

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.workflow.spworkflowactivationproperties_members.aspx
0
 

Accepted Solution

by:
scientiasro earned 0 total points
ID: 40319139
Hi,
yes that was the answer :-) I will paste the whole application of the solution.

At the beginning of the .cs file should be included references:
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;

Open in new window

Then further in the code:
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

Open in new window

The code is running when the workflow is activated:
private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
        {
         // workflowProperties.SiteUrl and .WebId returns not null value like SPContext.Current.Web
         using (SPSite site = new SPSite(workflowProperties.SiteUrl))
            {
                using (SPWeb web = site.OpenWeb(workflowProperties.WebId))
                {
                  // place your code here
                }
            }
        }

Open in new window

The code inside aforementioned snippet can be used finally with the help of MSDN web pages where they are using "SPWeb web = SPContext.Current.Web;".
For example it can be used for retrieving lists on the same site as the workflow runs in: http://msdn.microsoft.com/en-us/library/office/ms456030(v=office.14).aspx

Thank you for helping to sorted it out.
Michal
0
 

Author Closing Comment

by:scientiasro
ID: 40327532
My solution is more complete.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Working with SharePoint Data 8 46
Icons and Colors for Terms 3 24
Class object 2 26
Connection String 16 43
I used to be SharePoint evangelist in our company, so my Outlook always full of questions about how to do this, or where I can find that. One day I found such an email with the following question: "how to attach 3-State workflow (one of the workflow…
SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now