Exemple #1
0
        public SharePointOutput GetLIFullName(string listName)
        {
            SharePointOutput result = new SharePointOutput();

            if (_spo == null)
            {
                _spo = SpoAuthUtility.Create(new Uri(_siteUrl), _username, WebUtility.HtmlEncode(_password), false);
            }

            string odataQuery = $"_api/web/lists/GetByTitle('{listName}')";

            string digest = _spo.GetRequestDigest();

            Uri url = new Uri(String.Format("{0}/{1}", _spo.SiteUrl, odataQuery));

            // Set X-RequestDigest
            var webRequest = (HttpWebRequest)HttpWebRequest.Create(url);

            webRequest.Headers.Add("X-RequestDigest", digest);

            // Send a json odata request to SPO rest services to fetch all list items for the list.
            var response = HttpHelper.SendODataJsonRequest(url, "GET", null, webRequest, _spo);

            if (!response.Success)
            {
                result.Success   = false;
                result.Exception = new Exception($"Failed to process the action on Sharepoint. HTTP Staus Code - {response.Status} ");
            }
            else
            {
                var responseJson         = Encoding.UTF8.GetString(response.Response, 0, response.Response.Length);
                SharepointRestData spObj = null;
                try
                {
                    spObj = Searilizer.DeserilizeSPJsonString(responseJson);
                }
                catch (Exception ex)
                {
                    result.Success   = false;
                    result.Exception = new Exception("Failed to deserialize Sharepoint REST Json string. Please see Inner Exception", ex);
                    return(result);
                }
                result.SPRestData = spObj;
            }
            return(result);
        }
Exemple #2
0
        public SharePointOutput CreateListItem(string listName, Dictionary <string, string> fields)
        {
            SharePointOutput result = new SharePointOutput();

            try
            {
                var listItemFullName = GetLIFullName(listName);
                if (!listItemFullName.Success)
                {
                    throw listItemFullName.Exception;
                }
                if (_spo == null)
                {
                    _spo = SpoAuthUtility.Create(new Uri(_siteUrl), _username, WebUtility.HtmlEncode(_password), false);
                }

                //string odataQuery = $"/_api/web/lists/GetByTitle('Leads from Flow')/items({id})";
                string odataQuery = $"_api/web/lists/GetByTitle('{listName}')/items";

                var jsonDataString = string.Format("{{ '__metadata': {{ 'type': '{1}' }}, {0} }}", ConvertDictionaryToJsonFormat(fields), listItemFullName.SPRestData.d.ListItemEntityTypeFullName);

                byte[] content = ASCIIEncoding.ASCII.GetBytes(jsonDataString);

                string digest = _spo.GetRequestDigest();

                Uri url = new Uri(String.Format("{0}/{1}", _spo.SiteUrl, odataQuery));

                // Set X-RequestDigest
                var webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                webRequest.Headers.Add("X-RequestDigest", digest);

                //var webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                //webRequest.Headers.Add("X-RequestDigest", digest);
                //webRequest.Headers.Add("X-HTTP-Method","MERGE");
                //webRequest.Headers.Add("IF-MATCH", "*");


                // Send a json odata request to SPO rest services to fetch all list items for the list.
                var response = HttpHelper.SendODataJsonRequest(url, "POST", content, webRequest, _spo);
                if (!response.Success)
                {
                    result.Success   = false;
                    result.Exception = new Exception($"Failed to process the action on Sharepoint. HTTP Staus Code - {response.Status} ");
                }
                else
                {
                    var responseJson = Encoding.UTF8.GetString(response.Response, 0, response.Response.Length);

                    SharepointRestData spObj = null;
                    try
                    {
                        spObj = Searilizer.DeserilizeSPJsonString(responseJson);
                    }
                    catch (Exception ex)
                    {
                        result.Success   = false;
                        result.Exception = new Exception("Failed to deserialize Sharepoint REST Json string. Please see Inner Exception", ex);
                        return(result);
                    }
                    result.SPRestData = spObj;
                }
            }
            catch (Exception ex)
            {
                result.Success   = false;
                result.Exception = ex;
            }
            return(result);
        }