示例#1
0
        public async Task <List <XListItem> > GetItems(string CAML)
        {
            var client      = Context.GetClient();
            var request     = new HttpRequestMessage();
            Uri siteUrl     = new Uri(Context.SiteUrl);
            Uri getItemsUrl = new Uri(siteUrl,
                                      string.Format("/_api/web/lists/GetByTitle('{0}')/GetItems(query=@v1)?@v1={{\"ViewXml\":\"<View><Query>{1}</Query></View>\"}}",
                                                    Title, CAML));

            request.Method     = HttpMethod.Post;
            request.RequestUri = getItemsUrl;
            List <XListItem> listItems = null;
            await client.SendAsync(request).ContinueWith((taskwithmsg) =>
            {
                var response = taskwithmsg.Result;

                var responseContentTask = response.Content.ReadAsStringAsync();
                responseContentTask.Wait();
                string resPonseData = responseContentTask.Result;

                XElement element = XElement.Parse(resPonseData);

                listItems = element.Descendants().Where(x => x.Name.LocalName == "entry").Select(
                    (xmlElement) =>
                {
                    XListItem listItem = XListItem.ParseListItem(xmlElement.ToString());
                    listItem.List      = this;
                    return(listItem);
                }).ToList();
            });

            return(listItems);
        }
示例#2
0
        internal static XListItem ParseListItem(string resPonseData)
        {
            XElement x = XElement.Parse(resPonseData);

            x = XmlUtil.RemoveAllNamespaces(x);
            if (x.Name.LocalName == "error")
            {
                if (x.XPathSelectElement("/message") != null)
                {
                    throw new ArgumentException(x.XPathSelectElement("/message").Value);
                }
            }

            XListItem listItem = new XListItem
            {
                Title                  = x.XPathSelectElement("content/properties/Title").Value,
                Id                     = x.XPathSelectElement("content/properties/Id").Value,
                ContentTypeId          = x.XPathSelectElement("content/properties/ContentTypeId").Value,
                Modified               = x.XPathSelectElement("content/properties/Modified").Value,
                Created                = x.XPathSelectElement("content/properties/Created").Value,
                AuthorId               = x.XPathSelectElement("content/properties/AuthorId").Value,
                EditorId               = x.XPathSelectElement("content/properties/EditorId").Value,
                OData__UIVersionString = x.XPathSelectElement("content/properties/OData__UIVersionString").Value,
                //Attachments = x.Descendants().Where(xg => xg.Name.LocalName == "Attachments").First().Value,
                GUID = x.XPathSelectElement("content/properties/GUID").Value,
            };

            foreach (var field in x.Descendants().First(x2 => x2.Name.LocalName == "properties").Descendants())
            {
                listItem[field.Name.LocalName] = field.Value;
            }

            return(listItem);
        }
示例#3
0
        public async Task <XListItem> GetItemById(int Id)
        {
            var client = Context.GetClient();

            var request            = new HttpRequestMessage();
            Uri siteUrl            = new Uri(Context.SiteUrl);
            Uri getListItemByIdUrl = new Uri(siteUrl, string.Format("/_api/web/lists/GetByTitle('{0}')/items({1})", Title, Id));

            request.Method     = HttpMethod.Get;
            request.RequestUri = getListItemByIdUrl;
            XListItem listItem = null;
            var       result   = await client.SendAsync(request).ContinueWith((taskwithmsg) =>
            {
                var response            = taskwithmsg.Result;
                var responseContentTask = response.Content.ReadAsStringAsync();
                responseContentTask.Wait();
                string resPonseData = responseContentTask.Result;

                listItem      = XListItem.ParseListItem(resPonseData);
                listItem.List = this;
                return(listItem);
            });

            return(result);
        }