gajapathi ks

gajapathi ks

  • NA
  • 3
  • 1.5k

WCF Service method Need to be consume in SharePoint Webpart using Jquery

Oct 31 2012 4:09 AM
My Requirement is that in SharePoint Visual webpart if i click on the Likes Button for a photo it should increase the likes count in the List Column from client  side, here i am able to do when i use javascript, but coming to normal visitor, it is giving access denied error, so i tried to do impersonation in Javascript, and after some struggling i came to know that we cant impersonate in javascript, to do this we should go for creating a method in WCF which will be impersonated and trying to consume in JQuery, please help me with a sample example how to do this.

Here i am pasting the methods which i used in WCF

IGallery.cs
-----------------

[ServiceContract]
    public interface IGallery
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        string UpdateListData(string ID, string columnData);
    }

Gallery.cs
------------------

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Gallery : IGallery
    {
        public string UpdateListData(string listItemId, string columnData)
        {
            try
            {
                SPSite site = new SPSite(SPContext.Current.Site.Url);

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    using (SPSite osite = new SPSite(site.ID))
                    {
                        using (SPWeb web = osite.OpenWeb())
                        {
                            SPList list = web.Lists["PhotoGallery"];
                            if (Convert.ToInt32(listItemId.ToString()) != 0)
                            {
                                SPListItem item = list.Items.GetItemById(Convert.ToInt32(listItemId.ToString()));
                                if (item != null)
                                {
                                    item["LikesCount"] = columnData;

                                    item.Update();

                                }
                            }



                        }
                    }



                });
                return "Success";
            }
            catch (SPException)
            {
                return "Fail";
            }



        }
    }





Please help me ...
Thanks in Advanced
Gaja