Tag Archives: Social
Post to Newsfeed on behalf of another user (Server Object Model)
The below code will post to Newsfeed for any user account you specify. Activity appears the same as if the user manually posted. This could be helpful for populating Newsfeed from external activity, custom event receivers, workflow, and other developer sources. Enjoy! ![]()
Screenshot
Code
using Microsoft.Office.Server.Social;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Config
string siteUrl = "http://mysite";
string login = "i:0#.w|domain\\user";
using (SPSite site = new SPSite(siteUrl))
{
// Context
SPUser user = site.RootWeb.SiteUsers[login];
SPUserToken token = user.UserToken;
SPServiceContext ctx = SPServiceContext.GetContext(site);
using (new SPServiceContextScope(ctx))
{
// Init
UserProfileManager upm = new UserProfileManager(ctx);
UserProfile prof = upm.GetUserProfile(user.LoginName);
SPSocialFeedManager mgr = new SPSocialFeedManager(prof, ctx, token);
SPSocialPostCreationData post = new SPSocialPostCreationData();
// Text
post.ContentText = "hello world, look at {0}!";
post.UpdateStatusText = true;
// Link
SPSocialDataItem [] link = new SPSocialDataItem [1];
link[0] = new SPSocialDataItem();
link[0].ItemType = SPSocialDataItemType.Link;
link[0].Text = "My Cool Link";
link[0].Uri = new Uri("http://www.google.com");
post.ContentItems = link;
// Save
SPSocialThread thread = mgr.CreatePost(null, post);
}
}
}
}
}
References
How to Follow a site URL with PowerShell in SharePoint 2013
Background
While migrating from SharePoint 2010 to 2013 we needed to take MyLinks and replace with new Social Site Following. Given a CSV with user names and site URLs I researched PowerShell options for programmatically making users follow sites. Now we can pre-populate Follow Sites before launch. The below code did the trick. ![]()
Code
Function AddSiteToUserFollow ($siteUrl, $userName) {
# Load site and user profile service
$site = Get-SPSite $siteUrl
$serviceContext = Get-SPServiceContext($site)
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)
if ($profileManager.UserExists($userName)) {
# Load user profile
$userProfile = $profileManager.GetUserProfile($userName)
# Load social following manager
$followingManager = New-Object Microsoft.Office.Server.Social.SPSocialFollowingManager($userProfile, $serviceContext)
# Prepare following object
$actorInfo = New-Object Microsoft.Office.Server.Social.SPSocialActorInfo
$actorInfo.ContentUri = $site.Url
$actorInfo.AccountName = $userProfile.AccountName
$actorInfo.ActorType = "Site"
# Add to followed sites
$followingManager.Follow($actorInfo)
}
}
AddSiteToUserFollow "http://sp15" "demo\sptest1"
Screenshots
References
Support
If the target user does not yet have a MySite created you could see the below error. The fix is simply to have them visit MySite – or – to upgrade their site collection from 2010 to 2013.
Exception calling “Follow” with “1” argument(s): “No personal site exists for the current user, and no further information is available. Internal type name: Microsoft.Office.Server.UserProfiles.SocialDataStoreException. Internal error code: 1.”
Note Board–Comment on external sites
While reviewing the new social features I noticed this interesting hyperlink on the “My Profile” page. It allows users to bookmark a special link which they can use to extend “I like It” and “Tags & Notes” features beyond SharePoint to comment on external websites.
I was curious to look under the hood and understand how this could be done technically.
A complete JavaScript function is added to your bookmark in the URL field and executes when clicked. The browser context holds the external site’s detail and is passed to SharePoint as GET parameter in the URL. There is a safety check for SharePoint sites. If the dialog function exists (CORE.JS) then we can assume we are on a SharePoint page and display the dialog instead of a full page.
The special character escaping is very clever and gives us a single line of text which JavaScript can execute.
ORIGINAL
javascript:var%20d=document,l=d.location,e=encodeURIComponent,du='http:u002fu002fsp2010:8080u002f_layoutsu002fSocialDataFrame.aspx',u=du+'?Title='+e(d.title)+'&Src=bm'+'&DisplayUrl='+e(l.href)+'&Url='+e(l.href); if (typeof(OpenTagDialog)=='function') { OpenTagDialog(du);}else{ if(!window.open(u,null,'toolbar=0,status=0,resizable=0,scrollbars=1,width=650,height=400')) { l.href=u; }}void(0);SPACED FOR EASY READING
javascript:
var%20d=document,
l=d.location,
e=encodeURIComponent,
du='http:u002fu002fsp2010:8080u002f_layoutsu002fSocialDataFrame.aspx',
u=du+'?Title='+
e(d.title)+
'&Src=bm'+
'&DisplayUrl='+
e(l.href)+
'&Url='+
e(l.href);
if (typeof(OpenTagDialog)=='function') {
OpenTagDialog(du);
}
else
{
if(!window.open(u,null,'toolbar=0,status=0,resizable=0,scrollbars=1,width=650,height=400')) {
l.href=u;
}
}
void(0);
