Monday, November 7, 2016

When you use Entity Framework data model in Sharepoint (e.g. in custom web part with server object model) you may face with the following problem: when try to query database the following exception is thrown:

The underlying provider failed on Open

If you will search for solution for this problem most of suggestions in forums will say that you need to grant access for account of IIS application pool to the database. In Sharepoint however it is only half of solution. The problem is that in Sharepoint impersonation is used by default, which mean that if you just grant access to app pool account it still may not work, because actual code will run under account of currently authenticated user (check Thread.CurrentPrincipal property in order to know exact account). As in most cases we don’t want to explicitly grant database access for all users of our web application the simplest solution will be grant access to account of application pool as it is suggested and then switch current context user to account of application pool. The simplest way to do it in Sharepoint context is to use SPSecurity.RunWithElevatedPrivileges:

1: SPSecurity.RunWithElevatedPrivileges(

2: () =>

3: {

4:// query database via EF

5: }

6: );

In this example we don’t need to reopen SPSite under SPSecurity.RunWithElevatedPrivileges as context user switch will happen anyway. Also note that SPSecurity.RunWithElevatedPrivileges has effect only when code runs in context of Sharepoint web app, but not e.g. in console applications – in last case connection will be done with account of the user under which current console application is running.

Tuesday, November 1, 2016

As you probably know in Sharepoint it is possible to add standard List view web part on the page which is located on the same site with referenced list or doclib. But what if we need to use List view web part for displaying list from other sub site (not the one where web part is added)? By default it is not possible but after several additional steps it become possible. Here are these steps:

Create new test page on the site where referenced doclib is located

Add list view web part for appropriate list and publish the page

Open page in Sharepoint designer

Detach page from page layout

Edit the page and change <ExportControlledProperties> property from False to True

Save the page in Designer

Go back to browser and edit the page. Now Export option will be available for List view web part

Export web part to local file system

In browser open other sub site where you need to add exported web part

Open developer tools panel and in Console tab execute the following javascript code for getting id of the current web:
SP.ClientContext.get_current().load(SP.ClientContext.get_current().get_web())
SP.ClientContext.get_current().executeQueryAsync()
SP.ClientContext.get_current().get_web().get_id()

Copy web id from output

Open exported .webpart file and find <WebId> property. After export it will contain empty guid: 00000000-0000-0000-0000-000000000000

Replace it with guid copied from developer console and save .webpart file

Now again in browser go to sub site where you need to add exported web part and edit the page where web part should be added

About Me

I've created this blog for sharing my technical experience in software engineering. Most of posts will be dedicated to Sharepoint. But I will write also about another areas of software development for .Net platform. Hope it will be useful and will help you in your work.