Пример #1
0
        public ActionResult Documents(string libId)
        {
            var  info = new DocumentInfo();
            Guid libraryId;

            if (!Guid.TryParse(libId, out libraryId))
            {
                info.AddMessage("Cannot get documents invalid library id.");
                return(View(info));
            }

            var spContext = SharePointContextProvider.Current.GetSharePointContext(System.Web.HttpContext.Current);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                try
                {
                    SP.Web web = clientContext.Web;

                    var library = web.Lists.GetById(libraryId);
                    clientContext.Load(library);
                    clientContext.ExecuteQuery();

                    if (!DocumentTypeBaseId.Any(id => id == library.BaseTemplate))
                    {
                        info.AddMessage("Cannot get documents invalid library.");
                        return(View(info));
                    }

                    var camlQuery = new CamlQuery
                    {
                        ViewXml = "<View><Query><Where><Contains><FieldRef Name='FileLeafRef'/>" +
                                  "<Value Type='String'>.xsn</Value>" +
                                  "</Contains></Where></Query></View>"
                    };

                    var listItems = library.GetItems(camlQuery);
                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();

                    foreach (var item in listItems)
                    {
                        info.ItemInfo.Add(new ListItemInfo(item));
                    }
                    return(View(info));
                }
                catch
                {
                    info.AddMessage("There was an error while getting documents.");
                    return(View(info));
                }
            }
        }