Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在将图像上载到SharePoint时减少拥塞_C#_Sharepoint_Upload_Sharepoint Clientobject_Congestion Control - Fatal编程技术网

C# 如何在将图像上载到SharePoint时减少拥塞

C# 如何在将图像上载到SharePoint时减少拥塞,c#,sharepoint,upload,sharepoint-clientobject,congestion-control,C#,Sharepoint,Upload,Sharepoint Clientobject,Congestion Control,因此,我创建了以下脚本: class spImageUpload() { private static System.Collections.Generic.List<string> keywords; private static NetworkCredential credentials = new NetworkCredential(username, password, domain); private static C

因此,我创建了以下脚本:

class spImageUpload()
    {
        private static System.Collections.Generic.List<string> keywords;
        private static NetworkCredential credentials = new NetworkCredential(username, password, domain);
        private static ClientContext clientContext = new ClientContext(site name);
        private static Web site = clientContext.Web;
        private static List list = site.Lists.GetByTitle(listName);
        private static FileCreationInformation newFile = new FileCreationInformation();

        private static Image image = new Image();
        private static FileIO fo = new FileIO();

        public SharePointAccess()
        {
            sharepointLogin();
            uploadImage();
        }

        private static void updateFields()
        {
            //Loads the site list
            clientContext.Load(list);
            //Creates a ListItemCollection object from list
            ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
            //Loads the listItems
            clientContext.Load(listItems);
            //Executes the previous queries on the server
            clientContext.ExecuteQuery();

            //For each listItem...
            foreach (var listItem in listItems)
            {
                //Writes out the item ID and Title
                //Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]);
                //Loads the files from the listItem
                clientContext.Load(listItem.File);
                //Executes the previous query
                clientContext.ExecuteQuery();
                //Writes out the listItem File Name
                //Console.WriteLine("listItem File Name: {0}", listItem.File.Name);

                //Looks for the most recently uploaded file, if found...
                if (listItem.File.Name.Contains(fileName))
                {
                    title = fileName;
                    //Changes the Title field value
                    listItem["Title"] = title;
                    //Changes the Keywords field value using the keywords list
                    foreach (var keyword in keywords)
                    {
                        listItem["Keywords"] += keyword;
                        //Writes out the item ID, Title, and Keywords
                        //Console.WriteLine("Id: {0} Title: {1} Keywords: {2}", listItem.Id, listItem["Title"], listItem["Keywords"]);
                    }
                }
                //Remember changes...
                listItem.Update();
            }
            //Executes the previous query and ensures changes are committed to the server
            clientContext.ExecuteQuery();
        }

        private static void uploadImage()
        {
            try
            {
                fo.loadFile();

                foreach (var img in fo.lImageSet)
                {
                    Console.WriteLine("Image Name: {0}", img.getName());
                }

                foreach (var img in fo.lImageSet)
                {
                    DateTime start;
                    DateTime end;

                    start = DateTime.Now;
                    //Sets file path equal to the path value stored in the current image of lImageSet
                    filePath = img.getPath();
                    //Writes out to the console indicating what's been stored in filePath
                    Console.WriteLine("Image Path: {0}", filePath);
                    //Reads in the contents of the file
                    newFile.Content = System.IO.File.ReadAllBytes(filePath);
                    //Sets the file name equal to the name value stored in the current image of lImageSet
                    fileName = img.getName() + ".jpeg";
                    //Sets the URL path for the file
                    newFile.Url = fileName;
                    //Creates a List object of type String
                    keywords = new System.Collections.Generic.List<string>();
                    //For each keyword in the current image stored in lImageSet...
                    foreach (var keyword in img.lTags)
                    {
                        //...add that keyword to the newly created list
                        keywords.Add(keyword);
                    }
                    //Uploads the file to the picture library
                    Microsoft.SharePoint.Client.File uploadFile = list.RootFolder.Files.Add(newFile);
                    //Loads uploadFile method
                    clientContext.Load(uploadFile);
                    //Executes previous query
                    clientContext.ExecuteQuery();

                    //Calls the updateFields method to update the associated fields of the most recently uploaded image
                    updateFields();

                    end = DateTime.Now;
                    TimeSpan span = end.Subtract(start);
                    //Writes out to the console to indicate the file has finished being uploaded
                    Console.WriteLine("Uploaded: {0}", fileName + " Done!");
                    Console.WriteLine("Time Elapsed: {0}", span.Seconds + "seconds");

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private static void sharepointLogin()
        {
            try
            {
                //Loads credentials needed for authentication
                clientContext.Credentials = credentials;
                //Loads the site
                clientContext.Load(site);
                //Loads the site list
                clientContext.Load(list);
                //Executes the previous queries on the server
                clientContext.ExecuteQuery();
                //Writes out the title of the SharePoint site to the console
                Console.WriteLine("Title: {0}", site.Title);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
class spImageUpload()
{
私有静态System.Collections.Generic.List关键字;
私有静态NetworkCredential凭据=新的NetworkCredential(用户名、密码、域);
私有静态ClientContext=新ClientContext(站点名称);
私有静态网站=clientContext.Web;
私有静态列表=site.Lists.GetByTitle(listName);
私有静态FileCreationInformation newFile=新FileCreationInformation();
私有静态映像=新映像();
私有静态文件IO fo=新文件IO();
公共共享点访问()
{
sharepointLogin();
上传图像();
}
私有静态void updateFields()
{
//加载站点列表
clientContext.Load(列表);
//从列表创建ListItemCollection对象
ListItemCollection listItems=list.GetItems(CamlQuery.CreateAllItemsQuery());
//加载列表项
加载(listItems);
//在服务器上执行以前的查询
clientContext.ExecuteQuery();
//对于每个列表项。。。
foreach(listItems中的var listItem)
{
//写出项目ID和标题
//WriteLine(“Id:{0}标题:{1}”,listItem.Id,listItem[“Title”]);
//从列表项加载文件
Load(listItem.File);
//执行上一个查询
clientContext.ExecuteQuery();
//写出listItem文件名
//WriteLine(“listItem文件名:{0}”,listItem.File.Name);
//查找最近上载的文件,如果找到。。。
if(listItem.File.Name.Contains(文件名))
{
title=文件名;
//更改标题字段值
列表项[“标题”]=标题;
//使用“关键字”列表更改“关键字”字段值
foreach(关键字中的var关键字)
{
listItem[“关键字”]+=关键字;
//写出项目ID、标题和关键字
//WriteLine(“Id:{0}标题:{1}关键字:{2}”、listItem.Id、listItem[“标题”]、listItem[“关键字”]);
}
}
//记住改变。。。
Update();
}
//执行上一个查询并确保将更改提交到服务器
clientContext.ExecuteQuery();
}
私有静态void uploadImage()
{
尝试
{
fo.loadFile();
foreach(变量img,单位为fo.lImageSet)
{
WriteLine(“映像名:{0}”,img.getName());
}
foreach(变量img,单位为fo.lImageSet)
{
日期时间开始;
日期时间结束;
start=DateTime.Now;
//将文件路径设置为lImageSet当前图像中存储的路径值
filePath=img.getPath();
//写入控制台,指示存储在filePath中的内容
WriteLine(“映像路径:{0}”,文件路径);
//读入文件的内容
newFile.Content=System.IO.File.ReadAllBytes(文件路径);
//将文件名设置为lImageSet当前图像中存储的名称值
fileName=img.getName()+“.jpeg”;
//设置文件的URL路径
Url=fileName;
//创建字符串类型的列表对象
关键词=新系统.Collections.Generic.List();
//对于lImageSet中存储的当前图像中的每个关键字。。。
foreach(img.lTags中的var关键字)
{
//…将该关键字添加到新创建的列表中
关键词.添加(关键词);
}
//将文件上载到图片库
Microsoft.SharePoint.Client.File uploadFile=list.RootFolder.Files.Add(新文件);
//加载上载文件方法
Load(上传文件);
//执行上一个查询
clientContext.ExecuteQuery();
//调用updateFields方法来更新最近上载的图像的关联字段
updateFields();
end=DateTime.Now;
时间跨度=结束。减去(开始);
//写入控制台以指示文件已完成上载
WriteLine(“上传:{0}”,文件名+“完成!”);
WriteLine(“经过的时间:{0}”,span.Seconds+“Seconds”);
}
}
捕获(例外e)
{
Console.WriteLine(如ToString());
}
}
私有静态void sharepointLogin()
{
尝试
{
//加载身份验证所需的凭据
clientContext.Credentials=凭据;
//加载站点
clientContext.Load(站点);
//加载站点列表
clientContext.Load(列表);
//在服务器上执行以前的查询
clientContext.ExecuteQuery();
//将SharePoint网站的标题写入控制台
Console.WriteLine(“标题:
private static void uploadImage()
        {
            try
            {

                foreach (var img in lImageSet)
                {
                    Console.WriteLine("Image Name: {0}", img.getName());
                }

                foreach (var img in lImageSet)
                {
                    //Counter to track the number of images that have been uploaded
                    i++;


                    //For every 10 images that are uploaded, to reduce congestion, log out of SharePoint and log back in.
                    if (i % 10 == 0)
                    {
                        clientContext.Dispose();
                        sharepointLogin();
                    }

                    ....