在 SharePoint 中向帖子添加提及内容、标签以及网站和文档的链接

了解如何向微博帖子添加 SocialDataItem 对象,这些对象在 SharePoint 社交源中呈现为提及内容、标签或链接。

在社交源中,最简单形式的帖子内容只包含文本,但还可以添加对象,呈现为提及内容、标签或网站、SharePoint 网站和文档的链接。 为此,请将 SocialDataItem 对象添加到定义帖子的 SocialPostCreationData 对象的 ContentItems 属性。 帖子可以包含多个链接。

注意

若要将嵌入的图片、视频或文档添加到帖子的内容,请将 SocialAttachment 对象添加到 SocialPostCreationData.Attachment 属性。 有关详细信息,请参阅如何:在 SharePoint 中向帖子嵌入图像、视频和文档

本文所述 API 来自 .NET 客户端对象模型。 但是,如果您使用的是诸如 JavaScript 对象模型的其他 API,则对象名称或相应的 API 可能有所不同。 有关指向相关 API 文档的链接,请参阅其他资源

在 SharePoint 中使用代码示例向帖子添加链接的先决条件

本文中的代码示例演示如何向微博帖子中添加链接。 这些示例来自使用下列 SharePoint 程序集的控制台应用程序:

  • Microsoft.SharePoint.Client

  • Microsoft.SharePoint.Client.Runtime

  • Microsoft.SharePoint.Client.UserProfilies

有关如何设置开发环境和创建控制台应用程序的说明,请参阅 如何:使用 SharePoint 中的 .NET 客户端对象模型创建和删除帖子以及检索社交源

示例:在 SharePoint 中向帖子添加网站、SharePoint 网站和文档的链接

下面的代码示例发布包含指向网站、SharePoint 网站和文档的链接的帖子。 它演示了如何:

  • 创建表示链接的 SocialDataItem 对象。 每个实例设置链接类型的 SocialDataItemType 字段、链接的显示文本和链接 URI。

  • 向帖子文本中添加占位符可指示应在何处呈现链接的显示文本。

  • 将链接对象添加到用于创建帖子的 SocialPostCreationData 对象的 ContentItems 属性。

注意

目前,SharePoint 以相同的方式处理指向网站、SharePoint 网站和文档的链接,但最佳做法是选择 SharePoint 网站和文档 的“网站 类型”和“ 文档 类型”。

在社交源中,单击网站、SharePoint 网站或文档的链接后,将在单独的浏览器窗口中打开项。

注意

运行代码前,请先更改 URL 变量的占位符值。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

namespace IncludeLinksInPost
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace the following placeholder values with the actual values.
            const string serverUrl = "http://serverName/siteName/";
            const string websiteLinkUrl = "http://bing.com";
            const string siteLinkUrl = "http://serverName/siteName/";
            const string docLinkUrl = "http://serverName/Shared%20Documents/docName.txt";

            // Define the link to a website that you want to include in the post.
            SocialDataItem websiteLink = new SocialDataItem
            {
                ItemType = SocialDataItemType.Link,
                Text = "link to a website",
                Uri = websiteLinkUrl
            };

            // Define the link to a SharePoint site that you want to include in the post.
            SocialDataItem siteLink = new SocialDataItem
            {
                ItemType = SocialDataItemType.Site,
                Text = "link to a SharePoint site",
                Uri = siteLinkUrl
            };

            // Define the link to a document that you want to include in the post.
            SocialDataItem docLink = new SocialDataItem
            {
                ItemType = SocialDataItemType.Document,
                Text = "link to a document",
                Uri = docLinkUrl
            };

            // Add the links to the post's creation data.
            // Put placeholders ({n}) where you want the links to appear in the post text,
            // and then add the links to the post's content items.
            SocialPostCreationData postCreationData = new SocialPostCreationData();
            postCreationData.ContentText = "Check out this {0}, {1}, and {2}.";
            postCreationData.ContentItems = new SocialDataItem[3] {
                    websiteLink,
                    siteLink,
                    docLink
                };
            try
            {

                // Get the context and the SocialFeedManager instance.
                ClientContext clientContext = new ClientContext(serverUrl);
                SocialFeedManager feedManager = new SocialFeedManager(clientContext);

                // Publish the post. This is a root post to the user's feed, so specify
                // null for the targetId parameter.
                feedManager.CreatePost(null, postCreationData);
                clientContext.ExecuteQuery();
                Console.Write("The post was published.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.Write("Error publishing the post: " + ex.Message);
                Console.ReadLine();
            }
        }
    }
}

示例:在 SharePoint 中向帖子添加提及某人的内容

以下代码示例发布了一个提及某用户的帖子。 它演示了如何:

在社交源中,单击提及内容后,可重定向到被提及人员的“关于”页面。

注意

运行代码前,请先更改 serverURLaccountName 变量的占位符值。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

namespace IncludeMentionInPost
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace the following placeholder values with the actual values.
            const string serverUrl = "http://serverName/siteName/";
            const string accountName = @"domain\\name or email address";

            // Define the mention link that you want to include in the post.
            SocialDataItem userMentionLink = new SocialDataItem
            {
                ItemType = SocialDataItemType.User,
                AccountName = accountName
            };

            // Add the mention to the post's creation data.
            // Put a placeholder ({0}) where you want the mention to appear in the post text,
            // and then add the mention to the post's content items.
            SocialPostCreationData postCreationData = new SocialPostCreationData();
            postCreationData.ContentText = "{0} does great work!";
            postCreationData.ContentItems = new SocialDataItem[1] { userMentionLink, };

            try
            {

                // Get the context and the SocialFeedManager instance.
                ClientContext clientContext = new ClientContext(serverUrl);
                SocialFeedManager feedManager = new SocialFeedManager(clientContext);

                // Publish the post. This is a root post to the user's feed, so specify
                // null for the targetId parameter.
                feedManager.CreatePost(null, postCreationData);
                clientContext.ExecuteQuery();
                Console.Write("The post was published.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.Write("Error publishing the post: " + ex.Message);
                Console.ReadLine();
            }
        }
    }
}

示例:在 SharePoint 中向帖子添加标签

以下代码示例发布了一个包含标签的帖子。 它演示了如何:

在好友动态订阅源中,单击标签可重定向到该标签的"关于"页面。 如果该标签尚不存在,则服务器会创建它。

注意

运行代码前,请先更改 serverURLtagName 变量的占位符值。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

namespace IncludeTagInPost
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace the following placeholder values with the actual values.
            const string serverUrl = "http://serverName/siteName/";
            const string tagName = "#" + "tagName";

            // Define the link to a tag that you want to include in the post. If the tag is new, the
            // server adds it to the tags collection.
            SocialDataItem tagLink = new SocialDataItem
            {
                ItemType = SocialDataItemType.Tag,
                Text = tagName
            };

            // Add the tag to the post's creation data.
            // Put a placeholder ({0}) where you want the tag to appear in the post text,
            // and then add the tag to the post's content items.
            SocialPostCreationData postCreationData = new SocialPostCreationData();
            postCreationData.ContentText = "I like {0}.";
            postCreationData.ContentItems = new SocialDataItem[1] { tagLink };

            try
            {

                // Get the context and the SocialFeedManager instance.
                ClientContext clientContext = new ClientContext(serverUrl);
                SocialFeedManager feedManager = new SocialFeedManager(clientContext);

                // Publish the post. This is a root post to the user's feed, so specify
                // null for the targetId parameter.
                feedManager.CreatePost(null, postCreationData);
                clientContext.ExecuteQuery();
                Console.Write("The post was published.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.Write("Error publishing the post: " + ex.Message);
                Console.ReadLine();
            }
        }
    }
}

另请参阅