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; }
<?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>
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; } }
<?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>
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; }
Source: https://habr.com/ru/post/269499/