Share via


ドキュメント ライブラリ テンプレートのサンプル SharePoint アドイン

ECM.DocumentLibraries サンプルには、プロバイダー ホスト型のアドインを使用することによって、リストまたはドキュメント ライブラリを作成したり、それにコンテンツ タイプを割り当てたり、既定のコンテンツ タイプを削除したりする方法が示されています。

このソリューションは、以下の操作を行う場合に使用します。

  • リストまたはドキュメント ライブラリを作成し、既定のコンテンツ タイプを適用する場合。
  • カスタム フィールドのローカライズ バージョンの追加、保守、または実装についてより大きな制御が必要な場合。
  • リストまたはライブラリについての既定のコンテンツ タイプを削除する場合。
  • リストまたはライブラリを作成する際にライブラリの設定値を適用する場合。

はじめに

始めるにあたり、ECM.DocumentLibraries サンプル アドインを、GitHub の Office 365 Developer Patterns and Practices プロジェクトからダウンロードします。

注:

この記事で提供されるコードは、明示または黙示のいかなる種類の保証なしに現状のまま提供されるものであり、特定目的への適合性、商品性、権利侵害の不存在についての暗黙的な保証は一切ありません。

ECM.DocumentLibraries サンプル アドインにアクセスするユーザーは、リストを管理する権限を持っている必要があります。 Default.aspx.cs の DoesUserHavePermission メソッドは、ユーザーの権限を検査して、リストの管理が許可されていることを確認します。 ユーザーにリストを管理する権限がない場合、アドインのエラー メッセージがユーザーに表示されます。

private bool DoesUserHavePermission()
        {
            var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
            using (var ctx = spContext.CreateUserClientContextForSPHost())
            {
                BasePermissions perms = new BasePermissions();
                perms.Set(PermissionKind.ManageLists);
                ClientResult<bool> _permResult = ctx.Web.DoesUserHavePermissions(perms);
                ctx.ExecuteQuery();
                return _permResult.Value;
            }
        }

ECM.DocumentLibraries サンプル アドインの使用

このアドインを起動すると、次の図に示すような開始ページが表示されます。 ECM.DocumentLibraries の開始ページは、[サイト コンテンツ]>[アプリの追加]>[ドキュメント ライブラリ]>[詳細オプション] を選択したときに表示される、新しいドキュメント ライブラリを追加するためのページに似ていますが、1 つの違いがあります。 このアドインを起動すると、[ドキュメント テンプレート] リストに、カスタム ドキュメント ライブラリ テンプレート、IT ドキュメントおよび Contoso ドキュメントが表示されます。 ユーザーが [作成] を選択すると、新しいドキュメント ライブラリに選択済みのカスタム コンテンツ タイプが割り当てられます。

ECM.DocumentLibraries サンプル アドインの開始ページ

ECM.DocumentLibraries サンプル アドイン開始ページを示すスクリーン ショット。[ドキュメント テンプレート] ドロップダウン ボックスの一覧に、選択肢として IT ドキュメントが示されている。


ユーザーが [作成] を選ぶと、Default.aspx.cs の CreateLibrary_Click メソッドにより選択された既定テンプレートがチェックされ、ContentTypeManager.cs の CreateITDocumentLibrary または CreateContosoDocumentLibrary が呼び出されます。次のコードを参照してください。

protected void CreateLibrary_Click(object sender, EventArgs e)
        {
            try
            {
                var _spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
                var _templateSelectedItem = this.DocumentTemplateType.Value;
                var _libraryToCreate = this.GetLibraryToCreate();
                using (var _ctx = _spContext.CreateUserClientContextForSPHost())
                {
                    
                    _ctx.ApplicationName = "AMS ECM.DocumentLibraries";
                    ContentTypeManager _manager = new ContentTypeManager();
                    switch(_templateSelectedItem)
                    {
                        case "IT Document":
                            _manager.CreateITDocumentLibrary(_ctx, _libraryToCreate);
                            break;
                        case "Contoso Document":
                            _manager.CreateContosoDocumentLibrary(_ctx, _libraryToCreate);
                            break;
                    }
                 }

                Response.Redirect(this.Url.Value);
            }
            catch (Exception _ex)
            {
                throw;
            }
        }


次に、CreateContosoDocumentLibrary メソッドによって、次のコード サンプルで示されているように、以下のタスクが実行されます。

  • Managed Metadata Service でカスタム フィールドを作成します。
  • コンテンツ タイプを作成します。
  • カスタム フィールドをコンテンツ タイプに関連付けます。
  • このコンテンツ タイプのドキュメント ライブラリを作成します。
        public void CreateContosoDocumentLibrary(ClientContext ctx, Library library)
        {
            // Check the fields.
            if (!ctx.Web.FieldExistsById(FLD_CLASSIFICATION_ID)){
                ctx.Web.CreateTaxonomyField(FLD_CLASSIFICATION_ID, 
                                            FLD_CLASSIFICATION_INTERNAL_NAME, 
                                            FLD_CLASSIFICATION_DISPLAY_NAME, 
                                            FIELDS_GROUP_NAME, 
                                            TAXONOMY_GROUP, 
                                            TAXONOMY_TERMSET_CLASSIFICATION_NAME);
            }
            
            // Check the content type.
            if (!ctx.Web.ContentTypeExistsById(CONTOSODOCUMENT_CT_ID)){
                ctx.Web.CreateContentType(CONTOSODOCUMENT_CT_NAME, 
                                          CT_DESC, CONTOSODOCUMENT_CT_ID, 
                                          CT_GROUP);
            }

            // Associate fields to content types.
            if (!ctx.Web.FieldExistsByNameInContentType(CONTOSODOCUMENT_CT_NAME, FLD_CLASSIFICATION_INTERNAL_NAME)){
                ctx.Web.AddFieldToContentTypeById(CONTOSODOCUMENT_CT_ID, 
                                                  FLD_CLASSIFICATION_ID.ToString(), 
                                                  false);
            }
            CreateLibrary(ctx, library, CONTOSODOCUMENT_CT_ID);
          
        }


CreateContosoDocumentLibrary は、OfficeDevPnP.Core の一部である CreateTaxonomyField メソッドを呼び出します。 CreateTaxonomyField により、プロバイダー向けのホスト型アドインからマネージ メタデータ サービスの中にフィールドが作成されます。

public static Field CreateTaxonomyField(this Web web, Guid id, string internalName, string displayName, string group, TermSet termSet, bool multiValue = false)
		{
			internalName.ValidateNotNullOrEmpty("internalName");
			displayName.ValidateNotNullOrEmpty("displayName");
			termSet.ValidateNotNullOrEmpty("termSet");

			try
			{
				var _field = web.CreateField(id, internalName, multiValue ? "TaxonomyFieldTypeMulti" : "TaxonomyFieldType", true, displayName, group, "ShowField=\"Term1033\"");

				WireUpTaxonomyField(web, _field, termSet, multiValue);
				_field.Update();

				web.Context.ExecuteQuery();

				return _field;
			}
			catch (Exception)
			{
				/// If there is an exception, the hidden field might be present.
				FieldCollection _fields = web.Fields;
				web.Context.Load(_fields, fc => fc.Include(f => f.Id, f => f.InternalName));
				web.Context.ExecuteQuery();
				var _hiddenField = id.ToString().Replace("-", "");

				var _field = _fields.FirstOrDefault(f => f.InternalName == _hiddenField);
				if (_field != null)
				{
					_field.DeleteObject();
					web.Context.ExecuteQuery();
				}
				throw;

			}
		}

CreateContosoDocumentLibrary は、OfficeDevPnP.Core に含まれる CreateContentType メソッドを呼び出します。 CreateContentType は、新しいコンテンツ タイプを作成します。

public static ContentType CreateContentType(this Web web, string name, string description, string id, string group, ContentType parentContentType = null)
        {
            LoggingUtility.Internal.TraceInformation((int)EventId.CreateContentType, CoreResources.FieldAndContentTypeExtensions_CreateContentType01, name, id);

            // Load the current collection of content types.
            ContentTypeCollection contentTypes = web.ContentTypes;
            web.Context.Load(contentTypes);
            web.Context.ExecuteQuery();
            ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();

            // Set the properties for the content type.
            newCt.Name = name;
            newCt.Id = id;
            newCt.Description = description;
            newCt.Group = group;
            newCt.ParentContentType = parentContentType;
            ContentType myContentType = contentTypes.Add(newCt);
            web.Context.ExecuteQuery();

            // Return the content type object.
            return myContentType;
        }


CreateContosoDocumentLibrary は、OfficeDevPnP.Core の一部である AddFieldToContentTypeById メソッドを呼び出します。 AddFieldToContentTypeById は、フィールドをコンテンツ タイプに関連付けます。

public static void AddFieldToContentTypeById(this Web web, string contentTypeID, string fieldID, bool required = false, bool hidden = false)
        {
            // Get content type.
            ContentType ct = web.GetContentTypeById(contentTypeID);
            web.Context.Load(ct);
            web.Context.Load(ct.FieldLinks);
            web.Context.ExecuteQuery();

            // Get field.
            Field fld = web.Fields.GetById(new Guid(fieldID));

            // Add field association to content type.
            AddFieldToContentType(web, ct, fld, required, hidden);
        }

CreateContosoDocumentLibrary により、ContentTypeManager.cs の CreateLibrary メソッドが呼び出され、ドキュメント ライブラリが作成されます。 CreateLibrary メソッドにより、ドキュメント ライブラリの説明、ドキュメントのバージョン、関連するコンテンツ タイプなどのライブラリ設定値が割り当てられます。

private void CreateLibrary(ClientContext ctx, Library library, string associateContentTypeID)
        {
            if (!ctx.Web.ListExists(library.Title))
            {
                ctx.Web.AddList(ListTemplateType.DocumentLibrary, library.Title, false);
                List _list = ctx.Web.GetListByTitle(library.Title);
                if(!string.IsNullOrEmpty(library.Description)) {
                    _list.Description = library.Description;
                }

                if(library.VerisioningEnabled) {
                    _list.EnableVersioning = true;
                }

                _list.ContentTypesEnabled = true;
                _list.Update();
                ctx.Web.AddContentTypeToListById(library.Title, associateContentTypeID, true);
                // Remove the default Document Content Type.
                _list.RemoveContentTypeByName(ContentTypeManager.DEFAULT_DOCUMENT_CT_NAME);
                ctx.Web.Context.ExecuteQuery();
            }
            else
            {
                throw new Exception("A list, survey, discussion board, or document library with the specified title already exists in this website.  Please choose another title.");
            }
        }

CreateLibrary により、OfficeDevPnP.Core の一部である ListExtension の RemoveContentTypeByName が呼び出されます。 RemoveContentTypeByName により、ドキュメント ライブラリ上の既定のコンテンツ タイプが削除されます。

        public static void RemoveContentTypeByName(this List list, string contentTypeName)
        {
            if (string.IsNullOrEmpty(contentTypeName))
            {
                throw (contentTypeName == null)
                  ? new ArgumentNullException("contentTypeName")
                  : new ArgumentException(CoreResources.Exception_Message_EmptyString_Arg, "contentTypeName");
            }

            ContentTypeCollection _cts = list.ContentTypes;
            list.Context.Load(_cts);

            IEnumerable<ContentType> _results = list.Context.LoadQuery<ContentType>(_cts.Where(item => item.Name == contentTypeName));
            list.Context.ExecuteQuery();

            ContentType _ct = _results.FirstOrDefault();
            if (_ct != null)
            {
                _ct.DeleteObject();
                list.Update();
                list.Context.ExecuteQuery();
            }
        }

ドキュメント ライブラリを作成した後、ドキュメント ライブラリの [ライブラリの設定] に移動して、アドインによってドキュメント ライブラリに割り当てられた名前、説明、ドキュメントのバージョン設定、コンテンツ タイプ、およびカスタム フィールドを確認します。

アドインによって適用されるライブラリ設定値

名前、Web アドレス、説明のフィールドが強調表示されている [ドキュメント ライブラリ設定] ページのスクリーン ショット。

関連項目