Exemple #1
0
        /// <summary>
        /// This function gets a list of files from the relative path by the 'Type' if file.
        ///     It then displays the list to the user in a dialog box. The user will then select the
        ///     file to copy locally. When the clicks the ok button the selected file will be copied
        ///     to destination path (the argument to the function). Sets RetFileName property to the name
        ///     of the file it copied.
        ///
        ///  Please refer to the checkForReqProps metod for required properties.
        ///
        /// </summary>
        /// <param name="DestinationPath"></param>
        /// <returns></returns>
        public Boolean GetFileFromList(string DestinationPath)
        {
            Boolean            tmpRetVal         = true;
            ClientContext      ctx               = null;
            String             tmpPropList       = String.Empty;
            ListItemCollection tmpFileCollection = null;
            string             tmpPath;
            string             tmpPath2;
            String             absUrl = string.Empty;
            String             tmpStr = string.Empty;

            System.IO.FileStream OutFile;

            try
            {
                tmpPropList = checkForReqProps("GetFileFromList");
                if (tmpPropList != String.Empty)
                {
                    RetErrMessage = "The following Properties must be set: " + tmpPropList;
                    tmpRetVal     = false;
                }
                else
                {
                    tmpFileCollection = getFileListByType();
                    // load the grid in form
                    System.Windows.Forms.Form tmpForm = new frmSPFileToCopy();
                    DataGridView tmpListbox           = (DataGridView)tmpForm.Controls["dgvFilesToCopy"];
                    foreach (ListItem tmpItem in tmpFileCollection)
                    {
                        //Debug.WriteLine("file: " + tmpItem["FileLeafRef"].ToString() + "DocType: " + tmpItem["DocType"].ToString());
                        if (tmpItem[typFldName] != null && tmpItem[typFldName].ToString() == SPFileType)
                        {
                            if (tmpItem[DescFldName] == null)
                            {
                                tmpStr = " ";
                            }
                            else
                            {
                                tmpStr = tmpItem[DescFldName].ToString();
                            }

                            tmpListbox.Rows.Add(tmpItem["FileLeafRef"].ToString(), tmpStr);
                        }
                        if (tmpItem[typFldName] == null || SPFileType == string.Empty)
                        {
                            if (tmpItem[typFldName] == null)
                            {
                                tmpListbox.Rows.Add(tmpItem["FileLeafRef"].ToString(), tmpItem[DescFldName]);
                            }
                            else
                            {
                                tmpListbox.Rows.Add(tmpItem["FileLeafRef"].ToString(), tmpItem[DescFldName].ToString());
                            }
                        }
                    }

                    tmpForm.Text = SPFileType;

                    // load the form
                    DialogResult tmpDiagRes = tmpForm.ShowDialog();

                    ctx = new ClientContext(SPSiteURL);

                    tmpPath  = vSPRelPath + "/" + tmpListbox.SelectedRows[0].Cells["colFile"].Value.ToString();
                    tmpPath2 = tmpPath.Replace(" ", "%20");
                    var infile = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, tmpPath2);
                    OutFile = System.IO.File.Create(DestinationPath + "\\" + tmpListbox.SelectedRows[0].Cells["colFile"].Value.ToString());

                    infile.Stream.CopyTo(OutFile);
                    RetFileName = tmpListbox.SelectedRows[0].Cells["colFile"].Value.ToString();
                    OutFile.Dispose();
                    infile.Dispose();
                    tmpForm.Dispose();
                }
            }
            catch (Exception ex)
            {
                tmpRetVal     = false;
                RetErrMessage = ex.Message;
            }
            return(tmpRetVal);
        }