Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Using a Provider Hosted Application for SharePoint 2013, I am unable to ue the following lines in my C# code to get the current user

SPWeb web = SPControl.GetContextWeb(this.Context);
SPUser sUser = web.CurrentUser;

The error is that it says I am unable to find namespace.

I tried to add the following to the top of the page, but WebControls does not exist.

using Microsoft.SharePoint.WebControls;

Any ideas of what I'm misisng?

thanks! Graham.

share|improve this question
    
Start with using Microsoft.SharePoint –  Robert Lindgren May 16 '13 at 13:26
    
I am, I have that one declared as well, right above this one. –  Graham May 16 '13 at 13:35
add comment

1 Answer 1

up vote 0 down vote accepted

So I was able to figure this out.

I was calling the wrong classes. Web and User where the ones i needed, not SPUser and SPWeb.

I also needed to execute the query to fill up web and current user. Here is a snippit of my code to create a dictionary of some variables.

using (var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority)) {
            Web web = clientContext.Web;
            User currentUser = clientContext.Web.CurrentUser;

            clientContext.Load(web);  //, web => web.Title);
            clientContext.Load(currentUser);
            clientContext.ExecuteQuery();

            //get timestamp
            string ts = DateTime.Now.ToString("yyyyMMddHHmmssfff");

            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("name", currentUser.Title);
            dictionary.Add("email", currentUser.Email);
            dictionary.Add("timestamp", ts);
} 

With this code, I am also able to grab content from the 'web' variable.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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