Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
Created December 29, 2013 00:05
Contains JS functions to post to SharePoint social feed (part of search hover panel customization).
/// <reference path="jquery-1.8.3.min.js" />
"use strict";
Window.cob = Window.cob || {};
Window.cob.sharepoint = Window.cob.sharepoint || {};
Window.cob.sharepoint.jsdemos = Window.cob.sharepoint.jsdemos || {};
Window.cob.sharepoint.jsdemos.ui = Window.cob.sharepoint.jsdemos.ui || {};
// ensure SP.UserProfiles is loaded..
$.getScript("/_layouts/15/SP.js",
function () { $.getScript("/_layouts/15/SP.UserProfiles.js",
function () { }
);
}
);
$(document).ready(function () {
});
Window.cob.sharepoint.jsdemos.ui = {
currentHoverPanelItemId: '',
socialPostUiDiv: '',
socialPostMessageTextInput: '',
socialPostEditUiDiv: '',
socialPostSuccessUiDiv: '',
socialPostFailureUiDiv: '',
socialPostErrorMsgDiv: '',
socialPostUiProgress: '',
socialPostButtonContainer: '',
ShowHoverPanelTextBox: function (hoverPanelItemId) {
Window.cob.sharepoint.jsdemos.ui.currentHoverPanelItemId = hoverPanelItemId;
Window.cob.sharepoint.jsdemos.ui.socialPostUiDiv = '#' + hoverPanelItemId + '_cob-hp-socialpostui';
Window.cob.sharepoint.jsdemos.ui.socialPostMessageTextInput = '#' + hoverPanelItemId + '_cob-hp-posttext';
Window.cob.sharepoint.jsdemos.ui.socialPostEditUiDiv = '#' + hoverPanelItemId + '_cob-hp-socialposteditui';
Window.cob.sharepoint.jsdemos.ui.socialPostSuccessUiDiv = '#' + hoverPanelItemId + '_cob-hp-socialpostsuccessui';
Window.cob.sharepoint.jsdemos.ui.socialPostFailureUiDiv = '#' + hoverPanelItemId + '_cob-hp-socialpostfailureui';
Window.cob.sharepoint.jsdemos.ui.socialPostErrorMsgDiv = '#' + hoverPanelItemId + '_cob-hp-posterror';
Window.cob.sharepoint.jsdemos.ui.socialPostUiProgress = '#' + hoverPanelItemId + '_cob-hp-socialpostuiprogress';
Window.cob.sharepoint.jsdemos.ui.socialPostButtonContainer = '#' + hoverPanelItemId + '_cob-hp-postbutton';
// show div with textbox..
var postUiDiv = $(Window.cob.sharepoint.jsdemos.ui.socialPostUiDiv);
$(postUiDiv).fadeIn();
}
}
Window.cob.sharepoint.jsdemos.socialmgr = {
PostToCurrentUserFeed: function (contextItem) {
$(Window.cob.sharepoint.jsdemos.ui.socialPostUiProgress).fadeIn();
$(Window.cob.sharepoint.jsdemos.ui.socialPostButtonContainer).hide();
var messageText = $(Window.cob.sharepoint.jsdemos.ui.socialPostMessageTextInput).val();
var clientContext = SP.ClientContext.get_current();
var feedManager = new SP.Social.SocialFeedManager(clientContext);
// Link to the current item..
var linkDataItem = new SP.Social.SocialDataItem();
linkDataItem.set_itemType(SP.Social.SocialDataItemType.link);
linkDataItem.set_text(contextItem.Title);
linkDataItem.set_uri(contextItem.Path);
var socialDataItems = [linkDataItem];
// Create the post content.
var postCreationData = new SP.Social.SocialPostCreationData();
postCreationData.set_contentText(messageText + ' {0}.');
postCreationData.set_contentItems(socialDataItems);
// Publish the post. Pass null for the "targetId" parameter because this is a root post.
var resultThread = feedManager.createPost(null, postCreationData);
clientContext.executeQueryAsync(PostSucceeded, PostFailed);
}
}
function PostSucceeded(sender, args) {
$(Window.cob.sharepoint.jsdemos.ui.socialPostEditUiDiv).hide();
$(Window.cob.sharepoint.jsdemos.ui.socialPostSuccessUiDiv).fadeIn();
}
function PostFailed(sender, args) {
$(Window.cob.sharepoint.jsdemos.ui.socialPostErrorMsgDiv).text(args.get_message());
$(Window.cob.sharepoint.jsdemos.ui.socialPostEditUiDiv).hide();
$(Window.cob.sharepoint.jsdemos.ui.socialPostFailureUiDiv).fadeIn();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment