sharepoint 2013 Web part issue when call spweb

I am facing below issue on custom web part. I developed this web part in vs2010 and deployed into SharePoint 2013 and framework version is 3.5.

The Web application at could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

But this is working when use url like http://servename:8899/xxx.

I tried with by changing the build platform any CPU , x64 etc. but still i am facing same issue. Please provide any help to resolve this issue.

Access denied when trying to get SPListCollection out of SpWeb in BCS connector

I have a BCS connector.

In a constructor of its service class I want to access a SharePoint list.

using (var site = new SPSite("My site URL") {     using (var web = site.RootWeb)     {         var list = web.Lists.TryGetList("My List"); //Getting value error: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))!         ...     } } 

But when I try to access the list like in the code example above, Im getting the Getting value error: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))! error.

The account I’m using for crawling is the same account I’m using myself for SharePoint administration so I’m pretty sure it has access to the list since I’m the one who created it?

What is the cause of this access denied error and how can I resolve it?

SPWeb object properties turn to “null reference exception”

I am currently working a Farm Solution that is supposed to work with SharePoint 2016 and I am facing something that I can’t understand. Maybe someone could explain this phenomenon or point what I am missing.

Basically, I deploy a List instance through a Web Feature. When the feature is activated, if the list is empty, I fill it with some values through the Feature Receiver. Now, I only want users that are Site Administrators to add, edit or delete items in this list. To achieve this, I created an Event Receiver for this list where I perform various checks. Everything seems to work as expected: when the feature is turned on, the list is created and populated (checks in the Event Receiver performed well at that moment) and only Site Administrators can add, edit or delete items in this list afterwards.

My code would be like the following:

// Feature Receiver public override void FeatureActivated(SPFeatureReceiverProperties properties) {     try     {         SPSecurity.RunWithElevatedPrivileges(delegate         {             SPWeb web = properties.Feature.Parent as SPWeb;             SPList list = web.Lists.TryGetList("Information");              if (list.Items.Count == 0)             {                 SPListItem itemTitle = list.Items.Add();                 itemTitle["Title"] = "Name";                 itemTitle["SPWHInformationValue"] = "My Service";                 itemTitle["SPWHInformationDisplayFooter"] = true;                 itemTitle["SPWHInformationFooterOrder"] = 1;                 itemTitle.Update();             }         });     }     catch (Exception e)     {         // Logs     } }  // Event Receiver public override void ItemAdding(SPItemEventProperties properties) {     try     {         base.ItemAdding(properties);          string userLogin = properties.UserLoginName;         SPSite site = properties.Site;         SPWeb web = site.RootWeb;         SPUser user = web.EnsureUser(userLogin);          if (!user.IsSiteAdmin)         {             properties.ErrorMessage = "You don't have permission to perfom this action on list \"Information\".";             properties.Status = SPEventReceiverStatus.CancelWithError;         }     }     catch (Exception e)     {         // Logs     } } 

Beside that, I wanted to create a small “framework” to avoid, as much as possible, code duplication and to ease unit testing. In a few words, I have various Services are linked to one or more Repository. To use a service in an Event Receiver, for example, I call an Accessor object that references the different services.

So, in the list Event Receiver, the code looks something like so:

public override void ItemAdding(SPItemEventProperties properties) {     try     {         base.ItemAdding(properties);          if (!Accessor.ListService.IsUserAuthorizedToPerfomAction(properties))         {             properties.ErrorMessage = "You don't have permission to perfom this action on list \"Informations\".";             properties.Status = SPEventReceiverStatus.CancelWithError;             return;         }     }     catch (Exception e)     {         // Logs     } } 

Now, when this code is executed the first time, through the Feature Receiver, it works perfectly fine. But if it is executed a second time, when I try to add an item to list through the UI, I get a null reference exception for the SPWeb object. By setting a various breakpoints in Visual Studio, I can see that the SPWeb object still exists, but most of its properties is filled with “null reference exception” by that time.

I spent hours playing around with SPSecurity.RunWithElevatedPrivilege, trying to save the SPContext and the SPWeb object in properties, but each time, when the “EnsureUser” method is called for the second time, an exception is raised. I also noticed that if I disable the feature, empty the list and reactivate the feature, the same problem occurs.

I finally ended with some kind of workaround I don’t really like.

Here is the last version of my code for my Services and Repositories:

// ListService public bool IsUserAuthorizedToPerfomAction(SPItemEventProperties properties) {     try     {         bool result = false;         string userLogin = properties.UserLoginName;          if (string.IsNullOrEmpty(userLogin))         {             return result;         }          SPSecurity.RunWithElevatedPrivileges(delegate         {             Accessor.EnsureContext(properties.Site, properties.Site.RootWeb);             SPWHUser user = Accessor.UserService.GetUserByLoginName(userLogin);              if (string.IsNullOrEmpty(user.LoginName) || user.ID == 0)             {                 SPUser spUser = properties.Site.RootWeb.EnsureUser(userLogin);                 user = new SPWHUser                 {                     ID = spUser.ID,                     IsHiddenInUI = spUser.IsHiddenInUI,                     IsSiteAdmin = spUser.IsSiteAdmin,                     LoginName = spUser.LoginName                 };             }              if (user.IsSiteAdmin)             {                 result = true;             }         });          return result;     }     catch (Exception e)     {         // Logs         return false;     } }  // UserService public SPWHUser GetUserByLoginName(string loginName) {     try     {         if (string.IsNullOrEmpty(loginName))         {             return new SPWHUser();         }          SPUser user = (_repository as IUserRepository).EnsureUser(loginName);          if (user == null)         {             return new SPWHUser();         }          return new SPWHUser         {             ID = user.ID,             IsHiddenInUI = user.IsHiddenInUI,             IsSiteAdmin = user.IsSiteAdmin,             LoginName = user.LoginName         };     }     catch (Exception e)     {         // Logs         return new SPWHUser();     } }  // UserRepository public SPUser EnsureUser(string loginName) {     if (string.IsNullOrEmpty(loginName))     {         return null;     }      if (_webRepository == null)     {         return null;     }      try     {         SPUser user = null;          // !!! This line is the problem !!!         user = _web.EnsureUser(loginName);          return user;     }     catch (Exception ex)     {         // Logs         return null;     } } 

I cleaned the code a little bit, but the important part is here. The “_web” property in UserRepository is an SPWeb object. I also use my own struct where I save user’s information (SPWHUser), mostly for Unit Testing.

I also ended with something really ugly (I think), in my Accessor object where I try to save the SPContext and the SPWeb:

public static void EnsureContext(SPSite site, SPWeb web) {     SPSecurity.RunWithElevatedPrivileges(delegate     {         if (Context != null)         {             return;         }          using (SPSite spSite = new SPSite(site.Url))         {             using (SPWeb spWeb = spSite.OpenWeb(web.ServerRelativeUrl))             {                 Context = SPContext.GetContext(spWeb);                  (WebService.Repository as IWebRepository).Context = Context;                 (WebService.Repository as IWebRepository).Web = web;                  (SiteService.Repository as ISiteRepository).Context = Context;                 (SiteService.Repository as ISiteRepository).Site = site;             }         }     }); } 

Even with such a manipulation, the properties of the SPWeb object turn to “null reference exception” the second time.

Can someone tell me what I do wrong or what I don’t understand?

My thanks in advance.