Exemple #1
0
        public static System.IO.Stream ImportImage(string fileLeafRef)
        {
            System.IO.Stream stream = null;

            var spContext = SharePointContextProvider.Current.GetSharePointContext(CurrentHttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    List photoList = clientContext.Web.GetList("/sites/SD1/SiteAssets/");

                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = @"<View Scope='Recursive'>
                                            <Query>
                                                <Where>
                                                    <Eq>
                                                      <FieldRef Name='FileLeafRef'></FieldRef>
                                                      <Value Type='Text'>" + fileLeafRef + @"</Value>
                                                     </Eq>
                                                 </Where>
                                             </Query>
                                         </View>";
                    ListItemCollection photoCollection = photoList.GetItems(camlQuery);

                    clientContext.Load(photoCollection);

                    clientContext.ExecuteQuery();

                    ListItem item = photoCollection.ElementAt(0);

                    File file = item.File;

                    ClientResult <System.IO.Stream> data = file.OpenBinaryStream();

                    clientContext.Load(file);

                    clientContext.ExecuteQuery();

                    stream = data.Value;

                    //Wait for stream to get Executed by SharePoint
                    bool isLong = false;
                    while (isLong == false)
                    {
                        isLong = data.Value.Length is long;
                    }
                    //Wait for stream to get Executed by SharePoint
                }
                return(stream);
            }
        }