Sharepoint API将新项目发布到列表 [英] Sharepoint API Post a new Item to a List

查看:56
本文介绍了Sharepoint API将新项目发布到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Hellow



我想用post / rest(C#)命令向List中添加一个新项目SharePoint中。我可以从API获取数据 - >这是有效的。





pw被删除


          SecureString passWord = new SecureString();

            foreach(char c in"" .ToCharArray())

            {

                passWord.AppendChar(c);

            }


            HttpWebRequest endpointRequest =  (HttpWebRequest)HttpWebRequest.Create("");

            endpointRequest.Method =" GET";

            endpointRequest.Accept =" application / json; odata = verbose";

            endpointRequest.Credentials = new SharePointOnlineCredentials("",passWord);

            endpointRequest.Headers.Add("授权","承载"+""");

            HttpWebResponse endpointResponse =  (HttpWebResponse)endpointRequest.GetResponse();
$


           试试
            {

                WebResponse webResponse = endpointRequest.GetResponse();

               流webStream = webResponse.GetResponseStream();

                StreamReader responseReader = new StreamReader(webStream);

                string response = responseReader.ReadToEnd();

                

                



                responseReader.Close();

                Console.ReadLine();

            }¥b $ b            catch(例外X)

            {

$
            }




现在我想要将项目上传到列表中,并且响应是禁止的403 


            string json = string.Empty;



            SecureString passWord = new SecureString();

            foreach(char c in"" .ToCharArray())

            {

                passWord.AppendChar(c);

            }


            json =" {\" KlantNr \":\" 2563 \",\" Klant \":\" testmattijs \"}" ;;

            ASCIIEncoding encoder = new ASCIIEncoding();

            byte [] data = encoder.GetBytes(json);



            HttpWebRequest endpointRequest =(HttpWebRequest)HttpWebRequest.Create("");

            endpointRequest.Method =" POST";

            endpointRequest.Accept =" application / json; odata = verbose";

            endpointRequest.ContentType =" application / json; odata = verbose";

            endpointRequest.ContentLength = data.Length;

            endpointRequest.Credentials = new SharePointOnlineCredentials("",passWord);

            endpointRequest.Headers.Add("授权","承载"+""");

            



           试试
            {

               使用(Stream newStream = endpointRequest.GetRequestStream())

                {

                    newStream.Write(data,0,data.Length);

                    newStream.Flush();

                    newStream.Close();
$


                    HttpWebResponse response = endpointRequest.GetResponse()as HttpWebResponse;

                }¥b $ b            }¥b $ b            catch(例外X)

            {

                string s = X.Message.ToString();

            } 






解决方案


我建议您使用SharePoint CSOM,这样您就可以轻松实现这一目标。


您可以通过工具在Visual Studio中安装SDK-> NuGet包管理器 - >

包管理器控制台


https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM


CSOM对CRUD的示例代码。

 ListItemCreationInformationlistCreationInformation = newListItemCreationInformation(); 
ListItemoListItem = oList.AddItem(listCreationInformation) );
oListItem [" Title"] =" Hello World";
oListItem.Update();
clientContext.ExecuteQuery();

一些链接供您参考。


https://www.fmtconsultants。 com / sharepoint-online-client-side-object-model /


https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code


http://ramdotnetdeveloper.blogspot.com/2016/ 08 / crud-operation-in-sharepoint-2013.html


最好的问候,



Hellow

I want with post/rest (C#) command add a new item to a List in Sharepoint. I Can get data from the API -> this works.

pw are deleted

          SecureString passWord = new SecureString();
            foreach (char c in "".ToCharArray())
            {
                passWord.AppendChar(c);
            }

            HttpWebRequest endpointRequest =  (HttpWebRequest)HttpWebRequest.Create("");
            endpointRequest.Method = "GET";
            endpointRequest.Accept = "application/json;odata=verbose";
            endpointRequest.Credentials = new SharePointOnlineCredentials("", passWord);
            endpointRequest.Headers.Add("Authorization", "Bearer " + "");
            HttpWebResponse endpointResponse =  (HttpWebResponse)endpointRequest.GetResponse();

            try
            {
                WebResponse webResponse = endpointRequest.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();
                
                

                responseReader.Close();
                Console.ReadLine();
            }
            catch (Exception X)
            {

            }

now I want a upload a item to a list and the respons is Forbidden 403 

            string json = string.Empty;

            SecureString passWord = new SecureString();
            foreach (char c in "".ToCharArray())
            {
                passWord.AppendChar(c);
            }

            json = "{\"KlantNr\":\"2563\",\"Klant\":\"testmattijs\"}";
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] data = encoder.GetBytes(json);

            HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("");
            endpointRequest.Method = "POST";
            endpointRequest.Accept = "application/json;odata=verbose";
            endpointRequest.ContentType = "application/json;odata=verbose";
            endpointRequest.ContentLength = data.Length;
            endpointRequest.Credentials = new SharePointOnlineCredentials("", passWord);
            endpointRequest.Headers.Add("Authorization", "Bearer " + "");
            

            try
            {
                using (Stream newStream = endpointRequest.GetRequestStream())
                {
                    newStream.Write(data, 0, data.Length);
                    newStream.Flush();
                    newStream.Close();

                    HttpWebResponse response = endpointRequest.GetResponse() as HttpWebResponse;
                }
            }
            catch (Exception X)
            {
                string s = X.Message.ToString();
            } 

解决方案

Hi,

I suggest you use SharePoint CSOM so you could achieve this easily.

You could install SDK in Visual Studio by Tools->NuGet Package Manager-> Package Manager Console

https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM

Sample code to CRUD by CSOM.

ListItemCreationInformationlistCreationInformation = newListItemCreationInformation();
ListItemoListItem = oList.AddItem(listCreationInformation);
oListItem["Title"] = "Hello World";
oListItem.Update();
clientContext.ExecuteQuery();

Some links for your reference.

https://www.fmtconsultants.com/sharepoint-online-client-side-object-model/

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code

http://ramdotnetdeveloper.blogspot.com/2016/08/crud-operation-in-sharepoint-2013.html

Best Regards,

Lee


这篇关于Sharepoint API将新项目发布到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆