var ShareToLinkedIn = { reqId: null, authorizationStateChange: { get: function(target, name){ if(name === "isAuthorizationSuccess"){ return name in target ? target[name] : false; } throw new ReferenceError(name + " is not defined."); }, set: function (obj, prop, val) { obj[prop] = val; if(prop === "isAuthorizationSuccess"){ if (val) { ShareToLinkedIn.shareContent(); } } } }, initiateShare: function (reqId) { if (typeof IN === 'undefined') { alert("Cannot share to Linkedin at the moment!"); return; } this.reqId = reqId; var p = new Proxy(IN.User, ShareToLinkedIn.authorizationStateChange); if (!p.isAuthorizationSuccess) { IN.UI.Authorize().params({ "scope": ["r_basicprofile", "r_emailaddress", "w_share"] }).place(); } else { ShareToLinkedIn.shareContent(); } IN.Event.on(IN, 'auth', function () { if (!p.isAuthorizationSuccess) { p.isAuthorizationSuccess = true; } }); }, onSuccess: function (data) { alert("Successfully shared to your Linkedin profile!"); console.log(data); }, onError: function (error) { alert("Error occurred while sharing!"); console.log(error); }, // Use the API call wrapper to share content on LinkedIn shareContent: function () { if (this.reqId == null) { alert("Error occurred while sharing!"); return; } $.getJSON(window.$ROOT + "Advertisments/AdvertisementData", { id: this.reqId }) .done(function (data) { obj = data.Advertisement; var payload = { "comment": "Check out the new vacancies available at hSenidBiz Job Portal!", "content": { "title": obj[0].Name, "description": "Vacancies available for the post of " + obj[0].Name, "submitted-url": "", "submitted-image-url": "Attachment/"+this.reqId+".png" }, "visibility": { "code": "anyone" } } IN.API.Raw("/people/~/shares?format=json") .method("POST") .body(JSON.stringify(payload)) .result(ShareToLinkedIn.onSuccess) .error(ShareToLinkedIn.onError); }) .fail(function (jqXHR, textStatus, errorThrown) { RctShowError(jqXHR, textStatus, errorThrown); }); } } $(document).ready(function(){ $(document).on("click", ".share-ln", function () { var ReqId = $(this).data("reqid"); ShareToLinkedIn.initiateShare(ReqId); }); });