Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a Windows Application for scanning the document and store that document to a SharePoint document library. When I'm finished scanning, I want to select the SharePoint document library path in the setting page. How can I do this?

share|improve this question

2 Answers 2

The my idea is get library with ComboBox(controls), with this you can choose where you want save...

Example of use:

UploadFile("http://mysite.com", combobox.Text, "", "imgscanned.jpg", bmp);

Add 2 References in your project:

1.Microsoft.SharePoint.Client.dll
2.Microsoft.SharePoint.Client.Runtime.dll

In the code of the your Project:

using System.Drawing; //used to manipulate image
using System.IO;
using Microsoft.SharePoint.Client;

I have implemente three methods:

  1. populate dropdown in ComboBox, listing all the lists with type "Library".
  2. Upload File to specific library.
  3. Convert image to byte.

    public static void populateLibraryDropdown(string SiteUrl) // if you have combobox use this method to populate
    {
        using (ClientContext context = new ClientContext(SiteUrl))
        {
            Web web = context.Web;
            ListCollection objListCollection = web.Lists;
            foreach (List list in objListCollection)
            {
                if (list.BaseType == BaseType.DocumentLibrary)
                {
                   dropdownLibrary.Items.Add(list.Title);
                }
            }
        }
    }
    
    public static void UploadFile(string SiteUrl, string LibraryName, string folder, string fileName, Image bmp) // set LibraryName get value of dropdownLibrary "dropdownLibrary.Text"
    {
        using (ClientContext context = new ClientContext(SiteUrl))
        {
            Web web = context.Web;
            List objLibrary = web.Lists.GetByTitle(LibraryName);
            context.Load(objLibrary);
            context.ExecuteQuery();
    
            FileCreationInformation fci = new FileCreationInformation();
    
            byte[] fileContentArray = ImageToByte(bmp); // bmp is Image, and i assume you use the method to convert the image to byte.
    
            fci.Content = fileContentArray;
            fci.Overwrite = true;
            fci.Url = fileName; //you can set type of file
    
            if (string.IsNullOrEmpty(folder)) objLibrary.RootFolder.Files.Add(fci); //case folder is null, add in root folder of the list.
            else
            {
                string urlSubFolder = objLibrary.RootFolder.ServerRelativeUrl + "/" + folder;
                Folder subFolder = web.GetFolderByServerRelativeUrl(urlSubFolder);
                context.Load(subFolder);
                subFolder.Files.Add(fci); // add in folder specific
            }
            context.ExecuteQuery(); // execute to upload file.
        }
    }
    
    
    public static byte[] ImageToByte(Image img)
    {
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); // i use jpeg to stream but you can choose other format :)
            stream.Close();
            byteArray = stream.ToArray();
        }
        return byteArray;
    }
    

Sorry for my English.

Hope this helps.

share|improve this answer

As I understand, you are this application developer and these are your requirements. Then you should white code for this. The easyest way is to create textbox to allow user to paste document library url. For interaction with SharePoint you could use SharePoint Client Object Model, not SharePoint API as your App is running on a different computer.

The code you could use for this

public static void UploadFile(string siteUrl, string targetFolder, string fileName)
 {
      ClientContext context = new ClientContext(siteUrl);
      Web web = context.Web;
      Folder docSetFolder = web.GetFolderByServerRelativeUrl(targetFolder);
      context.ExecuteQuery();

      string documentUrl = "/" + targetFolder + "/" + fileName;

      FileCreationInformation fci = new FileCreationInformation();
      fci.Url = documentUrl;
      fci.Content = new byte[] { }; //byte[] take your stream and convert to byte array



      //get the folder's file collection
      FileCollection documentFiles = docSetFolder.Files;
      context.Load(documentFiles);
      context.ExecuteQuery();

      File newFile = documentFiles.Add(fci);
      context.Load(newFile);
      ListItem item = newFile.ListItemAllFields;
      context.Load(item);

      //start setting metadata here
      string contentTypeId = string.Empty;
      item["ContentTypeId"] = contentTypeId;
      item.Update();

      context.ExecuteQuery();


}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.