SharePoint で、投稿にイメージ、ビデオ、ドキュメントを埋め込む

SocialAttachment オブジェクトをミニブログの投稿に追加する方法を説明します。これらは、SharePoint ソーシャル フィードに埋め込みの画像、ビデオ、またはドキュメントとしてレンダリングされます。

ソーシャル フィードでは、最も単純な形式の投稿コンテンツにはテキストのみが含まれますが、埋め込みのイメージ、ビデオ、およびドキュメントを追加することもできます。 これを行うには、投稿を定義する SocialPostCreationData オブジェクトの Attachment プロパティを使用します。 投稿には、1 つの添付ファイルを含めることができます。この添付ファイルは、SocialAttachment オブジェクトで表されます。

注:

投稿のコンテンツにメンション、タグ、またはリンクを追加するには、 SocialDataItem オブジェクトを SocialPostCreationData.ContentItems プロパティに追加します。 詳細については、「方法: SharePoint でメンション、タグ、サイトおよびドキュメントへのリンクを投稿に含める」を参照してください。

この記事で説明する API は, .NET クライアント オブジェクト モデルのものです。 JavaScript オブジェクト モデルなど別の API を使用する場合は、オブジェクト名または対応する API が異なることがあります。 関連 API のドキュメントへのリンクは、「その他の技術情報」を参照してください。

添付ファイルを投稿に追加するコード例を使用するための前提条件

この記事のコード例は、イメージ、ビデオ、およびドキュメントの添付ファイルをミニブログの投稿に追加する方法を示しています。 これらの例は、次の SharePoint アセンブリを使用するコンソール アプリケーションのものです。

  • Microsoft.SharePoint.Client

  • Microsoft.SharePoint.Client.Runtime

  • Microsoft.SharePoint.Client.UserProfilies

この記事の例を使用するには、イメージ、ビデオ、およびドキュメントをアップロードする必要があります。 ビデオの例を使用するには、サーバーでビデオ機能が有効化されていて、ビデオ ファイルがアセット ライブラリに保存されている必要があります。 オンプレミス環境でドキュメントの例を使用するには、その環境で Office Online が構成されている必要があります。 詳細については、「 SharePoint でデジタル資産ライブラリを計画する 」および「 Office Online を使用するように SharePoint を構成する」を参照してください。

開発環境をセットアップしてコンソール アプリケーションを作成する方法については、「 方法: SharePoint で .NET クライアント オブジェクト モデルを使用して投稿を作成および削除し、ソーシャル フィードを取得する」を参照してください。

例: 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 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 の投稿にビデオを埋め込む

次のコード例では、埋め込みビデオを含む投稿を公開しています。 次の操作方法を示しています。

この例では、サーバーでビデオ機能が有効にされており、ビデオ ファイルがアセット ライブラリにアップロードされている必要があります。 詳細については、コード例を使用するための前提条件を参照してください。

コードを実行する前に、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 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<SocialAttachment> 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 の投稿にドキュメントを埋め込む

次のコード例では、埋め込みドキュメントを含む投稿を公開しています。 次の操作方法を示しています。

この例をオンプレミス環境で使用するには、その環境が Office Online を使用するように構成されている必要があります。 詳細については、コード例を使用するための前提条件を参照してください。 それ以外の場合は、「方法: 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 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<SocialAttachment> 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();
            }
        }
    }
}

関連項目