Calling Sharepoint 2013 methods as services using WCF

September 12, 2014

webservice can transfer dataset but not generic lists
where as wcf can transfer both dataset and lists

sp object model cant be accessed in normal appln.sp context will come only if appln is hosted in sp like gmc ,ecg applns.

for accessing sp object model in web appln u have to expose those sp methods as webservices in sp machine and consume them in the web appln.

host webservice in sharepoint and consuming (2007 sharepoint.using disco command)

sharepoint service network credentials authentication

how to add a new item to sharepoint list programmatically

addding files to doc library

document library

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS

host wcf service in sharepoint and consuming : (select sharpoint 2010 empty project sharepoint template.add service reference and not web reference)

authentication : anonymous enabled
error :The HTTP request is unauthorized with client authentication scheme ‘Negotiate’. The authentication header received from the server was ‘NTLM’.

wcf specified column is added

Failed to convert key to token – The public key for assembly ‘(null)’ was invalid when running the sn command to know the publickeytoken of an asembly

ERROR: When trying to know the Public key token fo the strong assembly using sn command you may receive an error as below:
Failed to convert key to token — The public key for assembly ‘(null)’ was inval id.

SOLUTION: This error comes because the command options are case sensitive. The option Tp is case sensitive.
C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -Tp \PendingTaskReport.dll

Error : Service Reference Error: Failed to generate code for the service reference
soln: Have to uncheck the Reuse types in all referenced assemblies from Configure service reference option

An unhandled exception of type ‘System.TimeoutException’ occurred in mscorlib.dll

Additional information: The request channel timed out while waiting for a reply after 00:00:59.8750656. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.


if u have people as column in document library or list then u have to insert value there like below (use elevateprivileges)

How to Insert?

Let us suppose, you need to add more than one user at single time in SharePoint List. Users’ values may come from an Active Directory or metadata. All the users are in a string as comma or semicolon separated value. Let us suppose users are in below format.

string usercontrolvalue = “dhananjay,arun,anoj,patra,mub”;

We need to inert these multiple users in a column of type People or Group in SharePoint list.

Step 1

First we need to split name of all the users from the string. Since in our case users are comma separated so below code will make a string array with users as value at different index.

string[] userarray = usercontrolvalue.Split(‘,’);

Step 2

Now we need to convert users as string to SPFieldUserValue. Below function is taking user name as string and converting that into SPFieldUserValue.

_Web variable is denoting current web where List is part of.

public SPFieldUserValue ConvertLoginName(string userid)
SPUser requireduser = _web.EnsureUser(userid);
SPFieldUserValue uservalue = new SPFieldUserValue(_web, requireduser.ID, requireduser.LoginName);
return uservalue;

Step 3

We need to make instance of SPFieldUserValueCollection

SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();

Step 4

For all the user; we need to convert that as SPFieldUserValue then add that them to SPFieldUserValueCollection instance

for (int i = 0; i < userarray.Length; i++)
SPFieldUserValue usertoadd = ConvertLoginName(userarray[i]);

Only we need to perform above said task. For your reference entire code as below,

Entire code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
namespace TestingMultipleUsre
public partial class _Default : System.Web.UI.Page
SPWeb _web;
SPSite _site;
SPList myList
protected void Page_Load(object sender, EventArgs e)
string usercontrolvalue = "dhananjay,arun,anoj,patra,mub";
_site = new SPSite("http://adfsaccount:2222/&quot;);
_web = _site.OpenWeb();
myList = _web.Lists["TestingUser"];
_web.AllowUnsafeUpdates = true ;
myList = _web.Lists["TestingUser"];
SPListItem item = myList.Items.Add();
SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
string[] userarray = usercontrolvalue.Split(',');
for (int i = 0; i cd c:\windows\syswow64\inetsrv

c:\Windows\SysWOW64\inetsrv>appcmd.exe list config -section:system.webServer/ser

c:\Windows\SysWOW64\inetsrv>appcmd.exe set config -section:system.webServer/serv
erRuntime /uploadReadAheadSize:”204800″
Applied configuration changes to section “system.webServer/serverRuntime” for “M

in vs command prompt :
at the solution/bin/debug folder type : sn -T “dllname” (for getting strong name) …u will get publickeytoken of that assembly
at the solution/bin/debug foder type : gacutil -i “dllname”(for keeping into gac)
at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI type disco http://skeltadtptest:1111/_vti_bin/final/service.asmx.

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

SqlServer Notes : How to RESET identity columns in SQL Server

August 27, 2014

DBCC CHECKIDENT(‘[table_name]‘, RESEED, [new_reseed_value])
— for example

Here product is the table name

SQL Server – Finding TCP Port Number SQL Instance is Listening on

August 13, 2014

By default SQL Server listens on TCP port number 1433, and for named instances TCP port is dynamically configured. There are several options available to get the listening port for SQL Server Instance.

Here are a few methods which we can use to get this information.

Method 1: SQL Server Configuration Manager

Method 2: Windows Event Viewer

Method 3: SQL Server Error Logs

Method 4: sys.dm_exec_connections DMV

Method 5: Reading registry using xp_instance_regread

Let’s see how you can use each of these methods in detail:

Method 1: SQL Server Configuration Manager:

Step 1. Click Start > All Programs > Microsoft SQL Server 2012 > Configuration Tools > SQL Server Configuration Manager

Step 2. Go to SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for

Step 3. Right Click on TCP/IP and select Properties


Step 4. In TCP/IP Properties dialog box, go to IP Addresses tab and scroll down to IPAll group.


If SQL Server if configured to run on a static port it will be available in TCP Port textbox, and if it is configured on dynamic port then current port will be available in TCP Dynamic Ports textbox. Here my instance is listening on port number 61499.

Method 2: Windows Event Viewer:

When SQL Server is started it logs an event message as ‘Server is listening on [ ‘any’ ‘ in windows event logs. Here will be actual port number on which SQL Server is listening.

To view this using Event Viewer:

Step 1. Click Start > Administrative Tools > Event Viewer.

Note: If Administrative Tools are not available on Start menu, go to Start > Control Panel > System and Maintenance > Administrative Tools > View event logs

Step 2. Navigate to Event Viewer > Windows Logs > Application

Step 3. Since huge amount of event are logged, you need to use filtering to locate the required logs. Right click on Application and select Filter Current Log…


Step 4. You can filter the events by Event ID and Event source. The event we are interested in has Event ID of 26022, and it’s source is SQL Server Instance. You need to filter by both Event ID and SQL Server Instance if you have multiple instances installed, for a single instance you can filter by Event ID only. Click on OK to apply the filter.


Step 5. Once the filter is applied, Locate message ‘Server is listening on [ ‘any’ …’. As we can see from below screenshot that SQL Server Instance is running on TCP Port 61499.


Method 3: SQL Server Error Logs:

When SQL Server is started it also logs an message to SQL Server Error Logs. You can search for port number in SQL Server Error Logs by opening SQL Server Error Log in notepad or via T-SQL using extended stored procedure xp_ReadErrorLog as below:

EXEC xp_ReadErrorLog 0, 1, N’Server is listening on’, N’any’, NULL, NULL, ‘DESC’

Result Set:

LogDate ProcessInfo Text
2013-03-21 13:34:40.610 spid18s Server is listening on [ 'any' 61499].
2013-03-21 13:34:40.610 spid18s Server is listening on [ 'any' 61499].

(2 row(s) affected)

As we can see from the output that SQL Server Instance is listening on 61499.

Note: This method does not work if SQL Server Error Logs have been cycled. See sp_Cycle_ErrorLog for more information.

Method 4: sys.dm_exec_connections DMV:

DMVs return server state that can be used to monitor SQL Server Instance. We can use sys.dm_exec_connections DMV to identify the port number SQL Server Instance is listening on using below T-SQL code:

SELECT local_tcp_port
FROM sys.dm_exec_connections
WHERE session_id = @@SPID

Result Set:


(1 row(s) affected)

As we can see from the output… same as above Smile

Method 5: Reading registry using xp_instance_regread:

Port number can also be retrieved from Windows Registry database.

We can use extended stored procedure xp_instance_regread to get port number information using below T-SQL code:

DECLARE @portNumber NVARCHAR(10)

EXEC xp_instance_regread
@rootkey = ‘HKEY_LOCAL_MACHINE’,
@key =
‘Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll’,
@value_name = ‘TcpDynamicPorts’,
@value = @portNumber OUTPUT

SELECT [Port Number] = @portNumber

Result Set:

Port Number

(1 row(s) affected)

As we can see … same as above Smile Smile

Note: The above code will only work if SQL Server is configured to use dynamic port number. If SQL Server is configured on a static port, we need to use @value_name = ‘TcpPort’ as opposed to @value_name = ‘TcpDynamicPorts’. – try catch finally notes

March 27, 2014

The code inside a finally block will get executed regardless of whether or not there is an exception.

Exception handling in C#. In the below code


//Throwing Error
//Getting Caught
int x =3;

int x = 5 ;

In Try after throwing an Error which is caught in Catch ,After that Finall Block Executes with Assignment of x=3. and after that next line x=5 is also executed.Program not getting terminated..What might be the reason ?

SqlServer notes : Inbuilt reports provided by Sqlserver itself

March 13, 2014

In Object Explorer


right click on server name and select Reports->Standard Reports->

Performance -Top Queries by Average CPU Time

SQL SERVER – Comma Separated Values (CSV) from Table Column

January 22, 2014

Is there a way to convert a single column record set such as


into ‘1,2,3’ string in SQL Server?

1st way :


CREATE FUNCTION [dbo].[fn_MyFunction]


    DECLARE @Delimiter CHAR(2) 
    SET @Delimiter = ', '

    SELECT @str = COALESCE(@str + @Delimiter,'') + AColumn
    FROM dbo.myTable



2nd Way :

select stuff( (select ',' + YourColumn
               from YourTable
               for xml path('')), 1, 1, '')
3rd Way :

Select @Out = COALESCE(@Out + ', ', '') + [YourColumn] from YourTable

print @Out

4th way Best one :

(SELECT ',' + s.Name
FROM HumanResources.Shift s
FOR XML PATH('')),2,200000) AS CSV


MS word tips

January 16, 2014

To change uppercase lowercase


Shift + F3


Get every new post delivered to your Inbox.