Michael A
Michael A

Reputation: 9900

SharePoint 2013 Can't load user profiles "HttpContext is in an assembly that is not referenced"

I'm trying to create a piece of code that will populate a list object with all of the users within the SharePoint user profile service.

I have added references for Microsoft.SharePoint, Microsoft.Office.Server.UserProfiles and have the following block of code:

using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using System.Web;

namespace code.runner.intranet.Dal
{
    public class SPUsersDal : BaseDal
    {

        public List<SPUsersEntity> FetchItems(string siteName)
        {
            try
            {
                PostEvent("Attempting to load users from: " + siteName, BaseExceptionEventArgs.ExceptionLevel.Debug);

                using (var site = new SPSite(siteName))
                {
                    using (var web = site.OpenWeb())
                    {
                        PostEvent("Successfully opened: " + web.Url, BaseExceptionEventArgs.ExceptionLevel.Debug);

                        var serviceContext = SPServiceContext.GetContext(site);
                        var userProfileManager = new UserProfileManager(serviceContext);

                        foreach (UserProfile userProfile in userProfileManager)
                        {
                            // ...your code
                        }
                    }

However the following lines:

var serviceContext = SPServiceContext.GetContext(site);
var userProfileManager = new UserProfileManager(serviceContext);

Are in error, with the message:

The type "HttpContext" is defined in an assembly that is not referenced. You must add a reference to assembly System.Web"

Since I already have a System.Web reference, I'm not sure why I'm receiving this. I also couldn't get Microsoft's own example to work (seen here: https://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofile.aspx), receiving the same error on the same objects.

What am I missing?

Upvotes: 0

Views: 721

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

There is a good chance SharePoint assemblies you are using built against .Net 2.0/3.5 assemblies and you are referencing .Net 4.x assemblies (or other way around).

If latest version of Framework (4.x) is not requirement switching project to older version can be a solution.

To do so change "Target Version" in properties of the project to 3.5 from 4.x and verify if you are using correct x86/x64 flavor.

Note: I believe SharePoint 2013 should be using 4.x version of Framework - make sure you have matching assemblies and target version in your project. See similar question for details - Reference to Microsoft.SharePoint.dll.

Upvotes: 1

Related Questions