0

We are developing a new blog post implementation and it will work like as microfeed. On the other hand we just wanted to implement mentioning feature and we wanted to implement it at the backend side.

But there is a small issue with SPSocialFeedManager.GetUnreadMentionCount Method.

public void Create(SPFieldUserValueCollection mentionedPeople, SPUser whoMentioned, MentionedItem mentionedItem)
    {
        var context = SPServiceContext.GetContext("siteUrl");
        var profileManager = new UserProfileManager(context);
        var postCreationData = new SPSocialPostCreationData();
        var socialDataList = new List<SPSocialDataItem>();
        var profile = profileManager.GetUserProfile(whoMentioned.LoginName);
        var feedManager = new SPSocialFeedManager(profile, context);

        foreach (var item in mentionedPeople)
        {
            var socialDataUser = new SPSocialDataItem();
            socialDataUser.ItemType = SPSocialDataItemType.User;
            socialDataUser.AccountName = item.User.LoginName.Substring(item.User.LoginName.IndexOf('|') + 1);
            socialDataList.Add(socialDataUser);
        }

        postCreationData.ContentText = GetContentText(socialDataList.Count);

        var socialDataLink = new SPSocialDataItem();
        socialDataLink.ItemType = SPSocialDataItemType.Link;
        socialDataLink.Text = mentionedItem.Title;
        socialDataLink.Uri = new Uri(mentionedItem.Url);
        socialDataList.Add(socialDataLink);

        postCreationData.ContentItems = socialDataList.ToArray();
        feedManager.CreatePost(null, postCreationData);
    }

    private string GetContentText(int mentionedPeopleCount)
    {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < mentionedPeopleCount; i++)
        {
            builder.Append("{").Append(i).Append("}");
            if (mentionedPeopleCount != i + 1)
            {
                builder.Append(", ");
            }
        }

        builder.Append(" have been mentioned on this news ").Append("{").Append(mentionedPeopleCount++).Append("}");

        return builder.ToString();
    }

This code above is creating a microfeed item for the mentioned user, there is no problem but when I try to use SPSocialFeedManager.GetUnreadMentionCount Method, I am seeing that It doesn't increment the unread mentions count.

What I tried:

1) I tried same implementation with CSOM. It is incrementing the unread mentions count but I am not able to impersonate current user. Thus it is using farm account. For example : @Mentioned by SP_WebApps.

2) I tried it with JSOM, It is working properly. There is no issue. But I need to implement it with SSOM.

Edit : I found the reason why it doesn't work.

Microsoft.Office.Server.Microfeed.SPMicrofeedStore.IncrementUnreadAtMentionCount method is not able find SPServiceContext.Current because I was trying to implement this solution in a Event Receiver.

Do you have any idea how I can implement it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.