コード例 #1
0
        /// <summary>
        /// Pull training photos from a SharePoint list and add them as a dictionary to be used for training purposess.
        /// </summary>
        /// <returns>Dictionary organized by person key of each person with their photos attached.</returns>
        public Dictionary<string, PhotoPerson> getTrainingPhotos()
        {
            Dictionary<string, PhotoPerson> trainingPhotos = new Dictionary<string, PhotoPerson>();

            using (ClientContext context = Login(SharePointURL))
            {
                try
                {
                    var list = context.Web.GetList(TrainingListURL);
                    var query = CamlQuery.CreateAllItemsQuery();

                    var result = list.GetItems(query);
                    ListItemCollection items = list.GetItems(query);
                    context.Load(items, includes => includes.Include(
                        i => i[TrainingPersonIdColumn],
                        i => i[TrainingFileColumn],
                        i => i[TrainingIdColumn]));

                    //now you get the data
                    context.ExecuteQuery();

                    //here you have list items, but not their content (files). To download file
                    //you'll have to do something like this:

                    foreach (ListItem item in items)
                    {
                        PhotoPerson person = null;
                        if (item[TrainingPersonIdColumn] != null)
                        {
                            string fullName = (string)item[TrainingPersonIdColumn];
                            // look for existing person
                            if (trainingPhotos.ContainsKey(fullName))
                            {
                                person = trainingPhotos[fullName];
                            }
                            else
                            {
                                person = new PhotoPerson();
                                person.Name = fullName;
                                person.ID = item.Id;
                            }

                            //get the URL of the file you want:
                            var fileRef = item[TrainingFileColumn];

                            //get the file contents:
                            FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef.ToString());

                            using (var memory = new MemoryStream())
                            {
                                byte[] buffer = new byte[1024 * 64];
                                int nread = 0;
                                while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    memory.Write(buffer, 0, nread);
                                }
                                memory.Seek(0, SeekOrigin.Begin);
                                Photo photo = new Photo();
                                photo.ID = item.Id.ToString();
                                photo.Image = memory.ToArray();
                                person.Photos.Add(photo);
                            }

                            trainingPhotos.Add(fullName, person);

                        }

                    }

                }
                catch (Exception e)
                {
                    throw;
                }
            }
            return trainingPhotos;
        }
コード例 #2
0
        /// <summary>
        /// Used for OCR tagger - pulls a generic list of photos with IDs.
        /// </summary>
        /// <returns>List of photos</returns>
        public List<Photo> getPhotosToTag()
        {
            List<Photo> photos = new List<Photo>();

            using (ClientContext context = Login(SharePointURL))
            {
                try
                {
                    var list = context.Web.GetList(PhotosToTagURL);
                    var query = CamlQuery.CreateAllItemsQuery();

                    var result = list.GetItems(query);
                    ListItemCollection items = list.GetItems(query);
                    context.Load(items, includes => includes.Include(
                        i => i[PhotoFileColumn],
                        i => i[PhotoIdColumn]));

                    //now you get the data
                    context.ExecuteQuery();

                    //here you have list items, but not their content (files). To download file
                    //you'll have to do something like this:

                    foreach (ListItem item in items)
                    {
                        Photo photo = new Photo();

                        //get the URL of the file you want:
                        var fileRef = item[PhotoFileColumn];

                        //get the file contents:
                        FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef.ToString());

                        using (var memory = new MemoryStream())
                        {
                            byte[] buffer = new byte[1024 * 64];
                            int nread = 0;
                            while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                memory.Write(buffer, 0, nread);
                            }
                            memory.Seek(0, SeekOrigin.Begin);

                            photo.ID = item.Id.ToString();
                            photo.Image = memory.ToArray();
                            photos.Add(photo);
                        }

                    }

                }
                catch (Exception e)
                {
                    throw;
                }
            }

            return photos;
        }