sharepoint页面嵌入_在 SharePoint 中向帖子嵌入图像、视频和文档

在 SharePoint 中向帖子嵌入图像、视频和文档Embed images, videos, and documents in posts in SharePoint

09/25/2017

本文内容

了解如何向微博帖子添加 SocialAttachment 对象,这些对象在 SharePoint 社交源中呈现为嵌入图片、视频和文档。Learn how to add SocialAttachment objects to microblog posts, which render as embedded pictures, videos, and documents in SharePoint social feeds.

在社交源中,最简单形式的帖子内容只包含文本,但还可以添加嵌入图片、视频和文档。In a social feed, the simplest form of post content contains only text, but you can also add embedded pictures, videos, and documents. To do this, you use the Attachment property on the SocialPostCreationData object that defines the post. 帖子可包含一个附件(由 SocialAttachment 对象表示)。Posts can contain one attachment, which is represented by a SocialAttachment object.

本文所述的 API 来自 .NET 客户端对象模型。如果您使用的是 JavaScript 对象模型等其他 API,则对象名称或相应 API 可能有所不同。有关指向相关 API 文档的链接,请参阅 其他资源。The API described in this article is from the .NET client object model. If you're using another API, such as the JavaScript object model, the object names or corresponding API might be different. See Additional resources for links to documentation for related APIs.

使用代码示例向帖子添加附件的先决条件Prerequisites for using the code examples to add attachments to a post

本文的代码示例演示了如何将图像、视频和文档附件添加到微博帖子中。这些示例来自使用以下 SharePoint 程序集的控制台应用程序:The code examples in this article show how to add image, video, and document attachments to microblog posts. These examples are from a console application that uses the following SharePoint assemblies:

Microsoft.SharePoint.ClientMicrosoft.SharePoint.Client

Microsoft.SharePoint.Client.RuntimeMicrosoft.SharePoint.Client.Runtime

Microsoft.SharePoint.Client.UserProfiliesMicrosoft.SharePoint.Client.UserProfilies

若要使用本文中的示例,需要上传图像、视频和文档。To use the examples in this article, you'll need to upload an image, a video, and a document. 若要使用视频示例,必须在服务器上启用视频功能,并将视频文件存储在资产库中。To use the video example, the video feature must be enabled on the server and the video file must be stored in an asset library. 若要在本地环境中使用文档示例,必须在环境中配置 Office Online。To use the document example in an on-premises environment, Office Online must be configured in the environment.

示例:在 SharePoint 中向帖子嵌入图像Example: Embed an image in a post in SharePoint

以下代码示例发布了一个包含嵌入式图像的帖子。它演示了如何执行以下操作:The following code example publishes a post that contains an embedded image. It shows how to:

Create a SocialAttachment object that represents the image. The SocialAttachment specifies the SocialAttachmentKind.Image field and the URI of the image file.

Add the image object to the Attachment property of the SocialPostCreationData object that's used to create the post.

运行代码前,请先更改 URL 变量的占位符值。Change the placeholder values for the URL variables before you run the code.

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 EmbedImageInPost

{

class Program

{

static void Main(string[] args)

{

// Replace the following placeholder values with the actual values.

const string serverUrl = "http://serverName/siteName/";

const string imageUrl = "http://serverName/Shared%20Documents/imageName.jpg";

// Define the image attachment that you want to embed in the post.

SocialAttachment attachment = new SocialAttachment()

{

AttachmentKind = SocialAttachmentKind.Image,

Uri = imageUrl

};

// Define properties for the post and add the attachment.

SocialPostCreationData postCreationData = new SocialPostCreationData();

postCreationData.ContentText = "Look at this!";

postCreationData.Attachment = attachment;

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 中向帖子嵌入视频Embed a video in a post in SharePoint

以下代码示例发布了一个包含嵌入式视频的帖子。它演示了如何执行以下操作:The following code example publishes a post that contains an embedded video. It shows how to:

Get the SocialAttachment object that represents the video attachment by using the SocialFeedManager.GetPreview method.

Add the video attachment to the Attachment property of the SocialPostCreationData object that's used to create the post.

本示例需要服务器启用视频功能,并将视频文件上载到资产库。有关详细信息,请参阅 使用代码示例的先决条件。This example requires the video features to be enabled on the server and the video file to be uploaded to an asset library. See the prerequisites for using the code examples for more information.

运行代码前更改 URL 变量的占位符值。Change the placeholder values for the URL variables before you run the code.

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 EmbedVideoInPost

{

class Program

{

static void Main(string[] args)

{

// Replace the following placeholder values with the actual values.

const string serverUrl = "http://serverName/siteName/";

const string videoUrl = "http://serverName/Asset%20Library/fileName?Web=1";

try

{

// Get the context and the SocialFeedManager instance.

ClientContext clientContext = new ClientContext(serverUrl);

SocialFeedManager feedManager = new SocialFeedManager(clientContext);

// Get the video attachment from the server.

ClientResult attachment = feedManager.GetPreview(videoUrl);

clientContext.ExecuteQuery();

// Define properties for the post and add the attachment.

SocialPostCreationData postCreationData = new SocialPostCreationData();

postCreationData.ContentText = "Look at this!";

postCreationData.Attachment = attachment.Value;

// 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 中向帖子嵌入文档Example: Embed a document in a post in SharePoint

以下代码示例发布了一个包含嵌入式文档的帖子。它演示了如何执行以下操作:The following code example publishes a post that contains an embedded document. It shows how to:

Get the SocialAttachment object that represents the document attachment by using the SocialFeedManager.GetPreview method.

Add the document attachment to the Attachment property of the SocialPostCreationData object that's used to create the post.

若要在本地环境中使用此示例,必须将环境配置为使用 Office Online。To use this example in an on-premises environment, your environment must be configured to use Office Online. 有关详细信息,请参阅使用代码示例的先决条件。

运行代码前,请先更改 URL 变量的占位符值。Change the placeholder values for the URL variables before you run the code.

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 EmbedDocumentInPost

{

class Program

{

static void Main(string[] args)

{

// Replace the following placeholder values with the actual values.

const string serverUrl = "http://serverName";

const string documentUrl = "http://serverName/Shared%20Documents/fileName.docx";

try

{

// Get the context and the SocialFeedManager instance.

ClientContext clientContext = new ClientContext(serverUrl);

SocialFeedManager feedManager = new SocialFeedManager(clientContext);

// Get the document attachment from the server.

ClientResult attachment = feedManager.GetPreview(documentUrl);

clientContext.ExecuteQuery();

// Define properties for the post and add the attachment.

SocialPostCreationData postCreationData = new SocialPostCreationData();

postCreationData.ContentText = "Post with a document.";

postCreationData.Attachment = attachment.Value;

// 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();

}

}

}

}

另请参阅See also

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值