Code example: Delete posts and replies from the social feed by using the SharePoint 2013 .NET client object model
The following code example deletes a post or a reply from the current
user's personal feed. It shows how to:
• Get the Personal feed type
for the current user by using the GetFeed method.
• Iterate through the threads in the feed to get information about the
root post and replies.
• Delete a root post, reply, or thread by using the DeletePost method
(deleting a root post deletes the whole thread).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;
namespace SocialFeedCSOM
{
class Program
{
static void Main(string[] args)
{
// Replace the following placeholder value with the target SharePoint server.
const string serverUrl = "http://serverName/";
// Get the client context.
ClientContext clientContext = new ClientContext(serverUrl);
// Get the SocialFeedManager instance.
SocialFeedManager feedManager = new SocialFeedManager(clientContext);
Console.WriteLine("\nCurrent user's personal feed:");
// Set the parameters for the feed content that you want to retrieve.
SocialFeedOptions feedOptions = new SocialFeedOptions();
// Get the target owner's feed (posts and activities) and
// then run the request on the server.
ClientResult<SocialFeed> feed = feedManager.GetFeed(SocialFeedType.Personal, feedOptions);
clientContext.ExecuteQuery();
// Create a dictionary to store the Id property of each post and
// reply. This code example stores the Id so you can select a post
// or a reply to delete.
Dictionary<int, string> idDictionary = new Dictionary<int, string>();
// Iterate through each thread in the feed.
for (int i = 0; i < feed.Value.Threads.Length; i++)
{
SocialThread thread = feed.Value.Threads[i];
SocialPost rootPost = thread.RootPost;
// Only keep posts that can be deleted.
if (rootPost.Attributes.HasFlag(SocialPostAttributes.CanDelete))
{
idDictionary.Add(i, rootPost.Id);
Console.WriteLine(string.Format("{0}. \"{1}\" has {2} replies.",
(i + 1), rootPost.Text, thread.TotalReplyCount));
// Get the replies.
// If a thread contains more than two replies, the server returns
// a thread digest that contains only the two most recent replies.
// To get all replies, call SocialFeedManager.GetFullThread.
if (thread.TotalReplyCount > 0)
{
foreach (SocialPost reply in thread.Replies)
{
// Only keep replies that can be deleted.
if (reply.Attributes.HasFlag(SocialPostAttributes.CanDelete))
{
i++;
idDictionary.Add(i, reply.Id);
SocialActor author = thread.Actors[reply.AuthorIndex];
Console.WriteLine(string.Format("\t{0}. {1} replied \"{2}\"",
(i + 1), author.Name, reply.Text));
}
}
}
}
}
Console.Write("\nEnter the number of the post or reply to delete. "
+ "(If you choose a root post, the whole thread is deleted.)");
string postToDelete = "";
int postNumber = int.Parse(Console.ReadLine()) - 1;
idDictionary.TryGetValue(postNumber, out postToDelete);
// Delete the reply and make the changes on the server.
ClientResult<SocialThread> result = feedManager.DeletePost(postToDelete);
clientContext.ExecuteQuery();
// DeletePost returns digest thread if the deleted post is not the
// root post. If it is the root post, the whole thread is deleted
// and DeletePost returns null.
if (result.Value != null)
{
SocialThread threadResult = result.Value;
Console.WriteLine("\nThe reply was deleted. The thread now has {0} replies.", threadResult.TotalReplyCount);
}
else
{
Console.WriteLine("\nThe post and thread were deleted.");
}
Console.ReadKey(false);
}
}
}
http://msdn.microsoft.com/en-gb/library/jj163977.aspx
or
using js
http://msdn.microsoft.com/en-us/library/jj164025.aspx
api to delete posts or get posts for any user
http://msdn.microsoft.com/en-gb/library/jj163237.aspx
I have read that its on the current user but i belive it should work on all users! if it doesnt and only on the current logged in user than thats where impersonation comes to action ;) to get the user your looking for :)
SharePoint -access to path is denied
look at my post in the link above to impersonat a user, when you have the SPSite and SPWeb under the user you can than proceed to delete the post :)
to impersonate you need to get the system token first and make sharepoint think that your that person, anything within the SPSite that your going to impersonate will be under that user and not the current user your using, the method below is getting the system account token with elevated privlages (for anonymous users):
/// <summary>
/// get the system token from the list of users to elevate spsite
/// </summary>
/// <param name="spSite"></param>
/// <returns></returns>
protected static SPUserToken GetSystemToken(SPSite spSite)
{
SPUserToken res = null;
bool oldCatchAccessDeniedException = spSite.CatchAccessDeniedException;
try
{
spSite.CatchAccessDeniedException = false;
res = spSite.SystemAccount.UserToken;
}
catch (UnauthorizedAccessException)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSPSite = new SPSite(spSite.ID))
res = elevatedSPSite.SystemAccount.UserToken;
// (***)
});
}
finally
{
spSite.CatchAccessDeniedException = oldCatchAccessDeniedException;
}
return res;
}
to use this method you would need to do the following:
SPUserToken sysToken = null;
using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
{
//get the system token from the method GetSystemToken and passing the spSite that you want to run under
sysToken = GetSystemToken(spSite);
using (SPSite impersonatedSite = new SPSite(siteUrl, sysToken))
{
//we are impersonating the [@"SHAREPOINT\SYSTEM"] account
using (SPWeb web = impersonatedSite.OpenWeb())
{
//do your code here as the impersonated account like updating a list
}
}
}
merge the two solutions together and youve got a winner :) , an admin/owner being able to delete any post by an any user within a microfeed. PS make sure that its only visable/accessible for admin/owner lol