Domanda

So I have created the following script:

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());
            }
        }
    }

And it works well for the most part. However, on random occasions I will get an error (at a random point in the list of images being uploaded) that "The connection was forcibly closed: a connection that was expected to stay open was closed by the server". I figure this is some sort of congestion issue, and I haven't been able to find many solutions to this problem on Google. So I was wondering if anyone knew of a way to reduce congestion on the server from the client side when uploading image files, or if there was a more efficient way to upload the images, or if a solution like logging out of the SharePoint site every 15 images, then logging back in would work? Thanks in advance for any help!

È stato utile?

Soluzione

Probably not the best solution, but a temporary resolution I found until someone offers a better one is simply logging out of SharePoint and logging back in every ten minutes....

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();
                    }

                    ....
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top