Console application – Change app.config based on build configuration

In many cases while writing a console application, we maintain all our application properties in app.config file which is the default configuration file added by Visual Studio IDE. Most of the times we keep all our environment related settings in this file which could vary from DEV to staging to PRD.

So when we try to release a build, we end up changing these values manually every time. Instead, follow the below simple steps by which you can target different app configuration file for the different build configurations in your project.

  1. Unload your project from your solution
  2. Right click the project node and edit .csproj file
  3. Search for the last PropertyGroup node and add the below node after that
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
   <AppConfig>App.Release.config</AppConfig>
</PropertyGroup>

The above element maps App.Release.Config as the default configuration file when publish a build in Release mode for x86 environments. If you want to targetAny CPU you can use the value AnyCPU instead of x86. You can add multiple nodes similar to this to target multiple build configurations and environments.

Show internal names of SharePoint List fields in List Settings page

This works only in chrome and you need tapermonkey extension for this to work.

Create a new script in tapermonkey with the below script –

// ==UserScript==
// @name         Internal Names
// @namespace    https://spuser.wordpress.com/
// @require      http://code.jquery.com/jquery-latest.js
// @version      0.1
// @description  try to take over the world!
// @author       You
// @include        */_layouts/15/listedit.aspx?*
// @grant        none
// ==/UserScript==
/* jshint -W097 */
'use strict';

// Your code here...
$(document).ready(function(){
    var counter = 0;
    $('table[summary]:last tr').each(function(){ 
        if(counter == 0){
            var th = '<th class="ms-vh2-nofilter">Internal Name</th>';
            $(this).append($(th));
            counter++;
        }
        else{
            var anchor = $(this).find('td').first().children('a');
            var internalName = $(unescape(anchor.attr('href')).split('Field=')).last()[0]
            var clone = $(anchor).clone();
            debugger;
            $(clone).text(internalName);
            td = '<td class="ms-vb2">' + $(clone)[0].outerHTML + '</td>';
            $(this).append($(td));
        }
    });
});

The above code adds a new column in the list columns table.

SharePoint 2010 – Check if user in a specific group using Client Object Model

This post is just a modification of the solution provided by Vadim Gremyachev for SharePoint 2013 here. I’ve modified the code to work with SharePoint 2010 (since getByName() is not available in SP 2010)

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
 try{
    var context = new SP.ClientContext.get_current();
    var currentWeb = context.get_web();
 
    var currentUser = context.get_web().get_currentUser();
    context.load(currentUser);
 
    var allGroups = currentWeb.get_siteGroups();
    context.load(allGroups);
 
    context.executeQueryAsync(
       function(sender, args){
          var count = allGroups.get_count();
          for(i = 0; i &lt; count; i++){
             var grp = allGroups.getItemAtIndex(i);
             //provide your group name
             if(grp.get_loginName() == groupName){
                var groupUsers = grp.get_users();
                //load users of the group
                context.load(groupUsers);
                context.executeQueryAsync(
                   function(sender2, args2){
                      var userInGroup = false;
                      var groupUserEnumerator = groupUsers.getEnumerator();
                      while (groupUserEnumerator.moveNext()) {
                         var groupUser = groupUserEnumerator.get_current();
                         //check if current user is in the group members
                         if (groupUser.get_id() == currentUser.get_id()) {
                            userInGroup = true;
                            break;
                         }
                      }
                      OnComplete(userInGroup);
                   },
                   function onFailureGrp(sender2, args2){
                      OnComplete(false);
                   }
               );
            }
         }
       },
       function onFailure(sender, args){
          OnComplete(false);
       }
    );
 }
 catch(e){
    OnComplete(false);
 }
}

And this is how you the function should be invoked –

function IsCurrentUserAManager() 
{
		try{
			IsCurrentUserMemberOfGroup("Managers", function (isCurrentUserInGroup) {
				if(!isCurrentUserInGroup)
				{
				    //user in group		    
				}
			});
		}
		catch(e){			
		}		
}

People picker not resolving entities in SharePoint 2010

In one of the applications I was working lately, one usual day I was informed that the people picker suddenly stopped working across the site. It sounded very weird at first. After reviewing all the changes that were done recently in the site I found that the custom master page the application was using was updated to support IE9 compatibility which was breaking all the people picker controls in all the pages. So changing back it to IE8 fixed the issue.

Not compatible –

<meta http-equiv=”X-UA-Compatible” content=”IE=9″/>

Change it to –

<meta http-equiv=”X-UA-Compatible” content=”IE=8″/>

“The Subscription Settings service and corresponding application and proxy needs to be running in order to make changes to these settings” – even after configuring subscription Settings Service

I completed configuring my SharePoint 2013 box yesterday and with at most curiosity of creating my first app I opened Visual Studio 2012 and create a SharePoint hosted app. And I was greeted with my first error message on SharePoint 2013.

Apps are disabled on the site‘.

I then learnt that the ‘App Management Service’ and the ‘Microsoft SharePoint Foundation Subscription Settings Service’ should be up and running before I can configure the App URLs in Central Administration and deploying apps.

(Note: You must create a separate app domain for deploying your apps before all these which I have done already following this article)

The former is created automatically while installing SP 2013 but to get the later running we have to do it manually by creating a service application. This can be done through the powershell –

add-pssnapin </code><code>"Microsoft.Sharepoint.Powershell"
$manaccount = Get-SPManagedAccount nlv\Administrator
Remove-SPServiceApplicationPool -Identity SettingsServiceAppPool
$appPoolService = New-SPServiceApplicationPool -Name SettingsServiceAppPool -Account $manaccount
$appService = New-SPSubscriptionSettingsServiceApplication -ApplicationPool $appPoolService -Name SettingsServiceApp -DatabaseName SettingsServiceDB
$proxyService = New-SPSubscriptionSettingsServiceApplicationProxy -ServiceApplication $appService
So now I see that both the services are up and running.
Image

So again I navigated to Apps -> Configure Apps URLs in CA and again daang! Same message –

‘The Subscription Settings service and corresponding application and proxy needs to be running in order to make changes to these settings’

I was googling for an hour and found everywhere that only these two service applications have to be started to configure app URLs. And finally I found the silly step which has to be done after creating subscription settings service application given by chaks here.

You have to do an IISRESET and bingo! Now you will able to configure App URLs in CA.

Image

Integrating activity feed of a Facebook page in your SharePoint site

For the past couple of days one of my colleagues was trying to integrate the activity feed of our client’s Facebook brand page in the client’s public portal. When I was looking into it I initially tried with the Activity Feed plugin provided in the Facebook plugins page.

Our requirement was to load all the activities that are happening in our Facebook page such as comments, posts, videos, likes etc. But when I was using the activity feed plugin I realized that the activity feed plugin only brings the Facebook page activity done from the site in which it is currently hosted. For example, if you have integrated the activity feed plugin in your site http://demo.yoursite.com/, the plugin only shows the activity that is created from http://demo.yoursite.com. But our intentions was to bring the complete set of activities happening in the Facebook page.

Thankfully I found out the Facebook Like Box which met our requirements. It fetches the completely activity feed of a Facebook page and integrates into your site. You can also comment and like posts from the plugin.

ASP.NET DataGrid – Export to CSV encoding issue

If you are exporting an asp.net grid to the CSV format you might see some special characters appearing the exported file. This is because of not setting the right encoding format. I initially tried with UTF8, UTF32 and Unicode and none of them exported the data correctly. Finally I set the encoding to default like this –

Response.ContentEncoding = Encoding.Default;

Finally the single quotes and hypens were appearing correctly in the CSV. Please not that you have to HtmlDecode your data if you have already HtmlEncoded it.