Pages

Monday 29 December 2014

Get Root Site Url in Javascript


     var RootSiteUrl = window.location.protocol + "//" + window.location.hostname+":"+window.location.port;   

Get Current User in SPSevices

 var userName = $().SPServices.SPGetCurrentUser({
                    fieldName: "Title",
                    debug: false
                });

Like and Unlike update list Item using Javascript

 update likes / Unlike
     function updatelike(Itemid) {      
            //get mentioned list id
             var listid = GetListId("Posts");
             //like updated for blogs
             EnsureScriptFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
                 var aContextObject = new SP.ClientContext();
                 Microsoft.Office.Server.ReputationModel.Reputation.setLike(
                 aContextObject,
                 listid,
                 Itemid,
                 true);
                 //put your like successed or not
                 aContextObject.executeQueryAsync(function () {
                     alert('like successed');                
                 }, function (sender, args) {
                     alert('like failed');                
                 });
             });
         }

get list id
        function GetListId(listName) {
            var id = "";
     
            $().SPServices({
                operation: "GetList",
                listName: listName,
                async: false,
               // webURL: rootSite,
                completefunc: function (xData, Status) {
                    id = $(xData.responseXML).find("List").attr("ID");
                }
            });
            return id;
        }   

Like / Unlike update list in SharePoint 2013

SPFieldUserValueCollection declare globally:

SPFieldUserValueCollection likedBy;

* set Like /Unlike
* give parameter value Itemid as string type and likeunlike boolean value
* true=like/false=unlike

public void setlike(string ItemID, bool Likeunlike)
        {        
            using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {                
                    SPList olist = oweb.Lists.TryGetList("Posts");
                    if (olist != null)
                    {
                        SPListItem item = olist.GetItemById(Convert.ToInt32(ItemID));

                        if (!string.IsNullOrEmpty(Convert.ToString(item["LikedBy"])))
                        {
                            likedBy = new SPFieldUserValueCollection(oweb, item["LikedBy"].ToString());
                        }
                        else
                        {
                            likedBy = new SPFieldUserValueCollection();

                        }
                            SPUser user = oweb.EnsureUser(SPContext.Current.Web.CurrentUser.Name);
                            SPFieldUserValue newUser = new SPFieldUserValue(oweb, user.ID, user.Name);
                            //Like
                            if (Likeunlike == true)
                            {                            
                                likedBy.Add(newUser);
                            }
                            //Unlike
                            else
                            {
                                SPFieldUserValue DeletedUserValue = likedBy.First(u => u.LookupId == user.ID);
                                likedBy.Remove(DeletedUserValue);
                            }

                            int likes = likedBy.Distinct().Count();
                            item["LikesCount"] = likes;
                            item["LikedBy"] = likedBy;
                            oweb.AllowUnsafeUpdates = true;
                            item.SystemUpdate(false);
                            oweb.AllowUnsafeUpdates = false;                  
                     
                    }
                }
            }
        }

Sunday 28 December 2014

Difference between public static string and public string


static will be the same across all sessions and persisted; the other will not be persisted across postbacks.