Friday, April 22, 2016

In server object model in Sharepoint there is simple way to get current list (i.e. list which is currently opened in browser window):

1: var list = SPContext.Current.List;

In javascript object model it is not so obvious, but also possible. In order to get current list you have to use not very well known function GetCurrentCtx() defined in core.js. With this function we can get current list like this:

In this example the main difference between context created with SP.ClientContext.get_current() and context returned from GetCurrentCtx() is that last one has ListTitle property set to title of the current list or document library. Having it we can easily get list object from the web. Also this context contains other useful properties like listBaseType, listName, listTemplate and listUrlDir.

It happens because of List Lookup Threshold Limit which is set to 12 in Sharepoint Online (see Increased site collection and list lookup limits). In order to avoid this error use the following workaround: at first read necessary item to get it’s id. Then load this item using List.getItemById method which will return list item containing all fields without need to explicitly enumerate them in “Include(…)” statement:

In this example we read all items from the list first and get id of the 1st item only, but you may retrieve item using your own criteria of course by specifying appropriate CAML query. With this approach it is possible to get values of all fields from the list in list item.

Thursday, April 14, 2016

Sometime ago we faced with interesting issue: we use JSONP request via jQuery.ajax() to send data to another domain:

1: jQuery.ajax({

2: url: url,

3: data: { ... },

4: dataType: "jsonp",

5: success: function(msg) {

6: ...

7: }

8: });

On most sites it works as expected, i.e. it sends HTTP GET request and passes JSONP callback function name in query string parameter. But on one site because of some reason it sends HTTP OPTION verb to the same url, which as you probably know, happens when using CORS. Since our endpoint didn’t support CORS we got errors:

In order to fix the issue, i.e. in order to force jQuery to use JSONP instead of CORS additional parameter “crossDomain: true” should be added to request options:

1: jQuery.ajax({

2: url: url,

3: data: { ... },

4: dataType: "jsonp",

5: crossDomain: true,

6: success: function(msg) {

7: ...

8: }

9: });

After that jQuery will start to use JSONP. However the question why it implicitly uses CORS on some sites even if we pass dataType: “jsonp” in request options is still open. If you will find the reason please share it in comments.

Wednesday, April 13, 2016

By default in Sharepoint users with Contribute permissions are not able to use scripting web parts. This is done because of security reasons, because otherwise regular contributors would be able to add e.g. ScriptEditorWebPart on the page and execute any javascript code on the page. This is how Media and Content web parts look like for contributors in this case:

As you can see neither ScripEditor nor ContentEditor web parts present here. At the same site users with Full control or Manage hierarchy permissions may add these web parts on the page:

However sometimes it is necessary to enable scripting web parts also for contributors. Fortunately in Sharepoint it is possible to enable this functionality for users with Contribute permissions. For on-premise Sharepoint steps are described in the following Technet article: Allow or prevent Contributors ability to edit scriptable Web Parts. You need to go to Central administration > Manage web applications > Select your web application and in the ribbon under Security category click Web part security:

For Sharepoint Online it is also possible to enable scripting web parts for contributors using the following instructions: Turn scripting capabilities on or off. As it says, scripting capabilities are disabled by default for:

Personal sites

Self-service created sites

The Root Site Collection of the Tenant

In admin center it is possible to enable scripting capabilities for the personal and self-service created sites:

For enabling this possibility on other site collections you need to execute the following PowerShell script:

Set-SPOsite <SiteURL> -DenyAddAndCustomizePages 0

But it should match to the settings specified in admin center, otherwise it will be overridden within 24h (admin center settings have priority over PowerShell). Hope that this informat

Wednesday, April 6, 2016

There are many guides of how to configure FBA in Sharepoint 2013 (e.g. here). One of the common problem with FBA in Sharepoint is that People picker doesn’t resolve FBA user names. How to fix this issue? Let’s use assumption that you followed configuration guide mentioned above and did all steps described there.

One of the most common solutions for this problem is that you need to add necessary permissions for identity of application pool of your web application to FBA database:

aspnet_Membership_FullAccess

aspnet_Personalization_FullAccess

aspnet_Profile_FullAccess

aspnet_Roles_FullAccess

aspnet_WebEvent_FullAccess

If you did it and it didn’t help try to search FBA user by full claims login name. E.g. suppose that we have user “superuser” in FBA database. Then claims login name will be “i:0#.f|fba_membership|superuser” (fba_membership is the name of membership provider configured in web.config of your web application, central administration and secure token service). It should find the user and after that People picker should successfully resolve FBA users.

Tuesday, April 5, 2016

Sometime we need to assign unique permissions on Sharepoint site. Suppose that we have Content Producers group and need to grant Contribute permission on the site. The following example shows how to do it using client object model:

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.