Search

SharePoint Components

Connect, Communicate, Collaborate

Tag

web service

Remove method name from JSON response in Web Service


I am working on a web service using SharePoint Rest Services. I have multiple GET methods where on request by a client my web service would send a JSON response.

Now what i want to send as a response is as below:


“id”: 1,
 “createdDate”: “1222”,
 “updatedDate”: “1222”

But what actually is sent as a response is as below:

{
“MethodNameResult”: {
“id”: 1,
“createdDate”: “1222”,
“updatedDate”: “1222”
}
}

Where MethodName is the name of the method where the response is being returned from.

After researching I found out that this can be avoided by setting the below value in the attribute.

BodyStyle = WebMessageBodyStyle.Bare

Example:

[OperationContract]
[WebInvoke(Method = “GET”, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = “/url”)]
Object MethodName();

Reference URL:

http://stackoverflow.com/questions/12733486/how-do-i-remove-the-method-node-from-the-response

This worked for me. Hope this helps you

Send Large files using SharePoint Webservice


I got an issue when I was sending large files to WCF REST service using custom SharePoint MultipleBaseAddressWebServiceHostFactory. The error displayed as

“Entity Request too Large” Response Code: 413

You cannot override the settings  in web.config because factory it self creates the dynamic end points so web.service changes can’t be used unless you remove the factory class and do it the conventional way.

Solution that worked for me:

You basically need to extend the below classes:

Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory 
Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHost
 public class CustomMultipleBaseAddressWebServiceHostFactory : Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory
    {
        protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new CustomMultipleBaseServiceHost(serviceType, baseAddresses);
        } 
    }
    public class CustomMultipleBaseServiceHost : Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHost
    {
        public CustomMultipleBaseServiceHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }
        protected override void OnOpening()
        {
            base.OnOpening();
            System.Diagnostics.Debug.Assert(false);
            foreach (ServiceEndpoint endpoint in this.Description.Endpoints)
            {
                Binding binding = endpoint.Binding;
                if (binding is WebHttpBinding)
                {
                    var web = binding as WebHttpBinding;
                    web.MaxBufferSize = Int32.MaxValue;
                    web.MaxReceivedMessageSize = Int32.MaxValue;
                }
                var myReaderQuotas = new XmlDictionaryReaderQuotas();
                myReaderQuotas.MaxStringContentLength = Int32.MaxValue;
                binding.GetType().GetProperty(“ReaderQuotas”).SetValue(binding, myReaderQuotas, null);
            }
        }
    }
Now instead of using default Microsoft class in the svc file we will use our custom dll reference.
<%@ ServiceHost Language=”C#” 
Service=”Custom.Assembly.Classname,$SharePoint.Project.AssemblyFullName$” 
CodeBehind=”CustomClass.cs”
Factory=”CustomMultipleBaseAddressWebServiceHostFactory, $SharePoint.Project.AssemblyFullName$”  %> 
Do an IISRESET and you will be able to send large files
Reference article:

Blog at WordPress.com.

Up ↑