1

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.

2
  • Start with using Microsoft.SharePoint Commented May 16, 2013 at 13:26
  • I am, I have that one declared as well, right above this one.
    – Graham
    Commented May 16, 2013 at 13:35

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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