Wednesday, September 18, 2013

Creating a Custom WCF Service in SharePoint Server

Creating a Custom WCF Service in SharePoint Server:-




Add This Code to IRevert.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService
{
    [ServiceContract]
    public interface IRevert
    {
        [OperationContract]
        void Revert(string listName, int listItemId);
    }
}

Add This Code to Revert.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService
{
    using Microsoft.SharePoint.Client.Services;
    using System.ServiceModel.Activation;
    using Microsoft.SharePoint;

    [BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class RevertService : IRevert
    {
        public void Revert(string listName, int listItemId)
        {
            SPList oList = SPContext.Current.Web.Lists[listName];

            SPListItem oItem = oList.GetItemById(listItemId);

            if (oItem.Versions.Count > 1)
            {
                oItem.Versions.Restore(1);
            }
        }
    }
}



<%@ServiceHost Language="C#" Debug="true"
    Service="WcfService.RevertService, $SharePoint.Project.AssemblyFullName$"
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


Add to this .csproj file:-
<TokenReplacementFileExtensions>svc</TokenReplacementFileExtensions>





Run below Command:
svcutil.exe http://<server-name>/_vti_bin/CustomWCF/Revert/Revert.svc/mex?wsdl
After run we can see generated two file in this path "C:\wcftest"
1. output.config file
2. RevertService.cs file 
 

Source:  http://msdn.microsoft.com/en-us/library/ff521581(v=office.14).aspx
Thank You.

1 comment:

  1. hi when i using the sample on SP 2013 i got one error message like this
    Sorry, something went wrong
    The type 'service.projet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e53c1eea8585e51', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

    ReplyDelete