Sharepoint 2013とのASP.NET MVCの統合。パート2:SharePointとの対話

前の記事、 ASP.NET MVCとSharepoint 2013の統合。パート1:信頼性の高いプロバイダーホストのAPPは 、SharePoint Apps用にSharePoint 2013を構成する方法を示しました (現在MicrosoftはそれをSharePointアドインと呼んでいます )。プロバイダーホスト型APPを備えたMVC。 この記事では、MVCアプリケーションでのSharePointサイト要素の検索、SharePointサイトからの要素の転送、App-parts、およびSharePoint要素のローカライズの実装方法を示します。



ASP.NET MVCアプリケーションを介してSharePointアイテムを検索する



アプリケーションのインターフェースのどこかに通常の検索フィールドがあり、その結果としてアクションSharepointSearchが呼び出されるとします。

public JsonResult SharepointSearch(string search) { var sharepointItems = GetSharePointSearchResult(search); var json = JsonHelper.ConvertToJsonResponse(sharepointItems); return Json(json); } private ResponseModel<List<SharePointDocumentModel>> GetSharePointSearchResult(string query) { var responseModel = new ResponseModel<List<SharePointDocumentModel>>(); var searchResult = new List<SharePointDocumentModel>(); var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); if (spContext == null) { responseModel.Initialize(searchResult, Resources.SharepointContextNull, ResponseStatusEnum.Fail); return responseModel; } using (var clientContext = spContext.CreateUserClientContextForSpHost()) { clientContext.ExecuteQuery(); var keywordQuery = new KeywordQuery(clientContext); keywordQuery.QueryText = query; var searchExecutor = new SearchExecutor(clientContext); var results = searchExecutor.ExecuteQuery(keywordQuery); clientContext.ExecuteQuery(); foreach (var resultRow in results.Value[0].ResultRows) { clientContext.ExecuteQuery(); var spDocument = new SharePointDocumentModel(); DateTime createdDateTime; DateTime.TryParse(GetDictonaryStringValueByKey(resultRow, "Write"), out createdDateTime); var contentTypeId = spDocument.DocumentName = GetDictonaryStringValueByKey(resultRow, "ContentTypeId"); if (contentTypeId.StartsWith("0x01")) { spDocument.DocumentName = GetDictonaryStringValueByKey(resultRow, "Title"); spDocument.DocumentUrl = GetDictonaryStringValueByKey(resultRow, "Path"); spDocument.Id = GetDictonaryStringValueByKey(resultRow, "WorkId"); spDocument.Author = GetDictonaryStringValueByKey(resultRow, "Author"); spDocument.CreateDate = createdDateTime.ToShortDateString(); searchResult.Add(spDocument); } } } responseModel.Initialize(searchResult); return responseModel; }
      
      





この場合、SPオブジェクトへのすべての可能な参照を取得する必要がありました(識別子 " 0x01 "による)。 ただし、 他のSharePointコンテンツ識別子を使用してアイテムの選択を絞り込むことができます。



CustomActionsを介してSharePointアイテムに参加する



SharePoint APPを使用して、CustomActions要素をSharePointに「埋め込む」ことができます(リボンのボタン、コンテキストメニューの要素)。 これを行う方法の詳細については、MSDNを参照してください



リボンのボタンとコンテキストメニュー項目の2つの要素を追加してみましょう。



コンテキストメニューから始めましょう。 これを行うには、新しいメニュー項目カスタムアクションをSharePoint APPアプリケーションに追加する必要があります。 要素を追加するウィザードで、Visual Studioはコンテキストメニュー項目を表示する場所とリストを選択するように求められます。 作成された要素を見ると、これが次の内容の通常のxmlファイルであることが明らかになります。

 <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <CustomAction Id="6d471c89-41cc-4b81-b794-ea7664dc4c38.AttachToNewDocument" RegistrationType="ContentType" RegistrationId="0x0101" Location="EditControlBlock" Sequence="10001" Title="$Resources:ContextMenu_AttachToNewDocument"> <UrlAction Url="~remoteAppUrl/Navigator/SharepointAction/?SPHostUrl={HostUrl}&SPListId={ListId}&SPListItemsId={ItemId}" /> </CustomAction> </Elements>
      
      





主な要素を順番に分析してみましょう。

RegistrationTypeおよびRegistrationId-メニュー項目が呼び出される組み合わせを示します(ウィザードで示されたもの。より広い範囲のアクションに対して再定義可能)

場所 -アイテムがある場所(コンテキストメニュー)

シーケンス -メニュー順序

タイトル -表示されるタイトル。 ここで、ローカライズがすでに適用されていることがわかります。 これは、 リソース(ホストWeb)から要素をローカライズする方法です。

UrlActionはCustomActionsの基盤です。 これは、クリック後にユーザーがリダイレクトされるURLです。 この場合、プロバイダーがホストするアプリ〜remoteAppUrlに特別なシステム指定が示されます。

また、パラメータSPListId、SPListItemsIdに注意することも重要です。これらは、それぞれ現在のシートとその中で選択された要素の識別子です。 マーカーによる値の置換方法。 そのようなリンクの形成に関する完全な情報は、 ここにあります。



それでは、MVCアプリケーションのホストアクションに移りましょう。

  public ActionResult SharepointAction(SharepointActionModel actionModel) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); GetSharepointListItemsAndPerformAction(spContext, actionModel.SpListId.Value, actionModel.SpListItemsId, (listItems, ids, context, spList) => { if (spList.BaseType == BaseType.DocumentLibrary) { richCardId = AttachSharepointDocumentsToDocumentCard(listItems, ids, context); } }); return Redirect(model.UrlToNavigate); } public class SharepointActionModel { public Guid? SpListId { get; set; } public string SpListItemsId { get; set; } public string SpHostUrl { get; set; } } private const string DocumentCamlQuery = @"<QueryOptions><ViewAttributes Scope='All'/></QueryOptions> <Where> <In> <FieldRef Name='ID' /> <Values> {0} </Values> </In> </Where>"; private void GetSharepointListItemsAndPerformAction(SharePointContext spContext, Guid listId, string listItemIds, Action<ListItemCollection, List<int>, ClientContext, List> fileAction) { try { if (spContext == null) throw Error.SharepointIntergration(); using (var clientContext = spContext.CreateUserClientContextForSPHost()) { var spList = clientContext.Web.Lists.GetById(listId); clientContext.Load(spList); clientContext.ExecuteQuery(); if (spList != null && spList.ItemCount > 0) { var camlQuery = new CamlQuery(); camlQuery.ViewXml = String.Format(DocumentCamlQuery, GetFilterValues(listItemIds)); var listItems = spList.GetItems(camlQuery); clientContext.Load(listItems); clientContext.ExecuteQuery(); var stringIds = listItemIds.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); var ids = ConvertToIntFromStringIds(stringIds); fileAction(listItems, ids, clientContext, spList); } } } catch (Exception ex) { Trace.TraceError(ex); throw; } }
      
      





次に、リボンのボタンを使用して、プロジェクトにリボンカスタムアクション要素を追加して同じことを行う必要があります。 また、URLを同じ場所に向けることができます。事前にSPListItemsIdにコンマで区切られた複数の要素が存在する可能性があります。 上記のコードでは、これはすでに提供されています。



SharePoint AppParts



AppPartは、アプリを介してSharePointにインストールできる別のアイテムです。 ただし、この場合、SharePointユーザーは、この要素をページのどこに追加するかを決定します。 AppPartの主なアイデアは、iframeテクノロジーを使用して、信頼性の高いアプリケーションの一部を表示することです。 以前のバージョンのSharePointでは、これもWebPartsを通じて実装されていました。



そのため、App-partの追加方法についてもMSDNで詳しく説明されています



Visual Studioウィザードを使用して彼のプロジェクトを再度追加できます。また、ほぼ次の内容のxmlファイルです。

 <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <ClientWebPart Name="Approvals" Title="$Resources:AppPart_Approvals_Title" Description="$Resources:AppPart_Approvals_Description" DefaultWidth="850" DefaultHeight="150"> <Content Type="html" Src="~remoteAppUrl/Navigator/ApprovalAppPart?{StandardTokens}" /> </ClientWebPart> </Elements>
      
      





コンテンツのサイズを変更するときは、アプリパーツのサイズを変更する必要があります。 アプリケーションのASP.NET MVC側でコンテンツのサイズを変更するときに手動で呼び出す必要があるJavaScriptコードを以下に残します。

  function ResizeIFrame() { if (!IsSharepointAppPart()) return; var oBody = document.body; var innerHeight = $(".app-part-content", oBody).height(); var dheight = innerHeight + (oBody.offsetHeight - oBody.clientHeight); var dwidth = oBody.scrollWidth + (oBody.offsetWidth - oBody.clientWidth); var message = "<Message senderId=" + senderId + " >" + "resize(" + dwidth + "," + dheight + ")</Message>"; window.parent.postMessage(message, document.referrer); } function IsSharepointAppPart() { return IsInsideIframe(); } function IsInsideIframe() { return window.self !== window.top; }
      
      





おそらくこれで終わりです。 次回、リボンタブ、SharePointソリューションを作成し、SharePointアプリにリンクする方法を書きます。



All Articles