using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Microsoft.SharePoint.Client; using System.Security; namespace SharepointEDMSSample { // // Class to read the input data without presuming where that data comes from; it could be a csv file, // a database, or something else. The code for this class should be replaced by code to read the data from your source. // // The code for this instance of this class calls Sharepoint or Sharepoint Online to get the contents of a Document Library // // This code relies on Sharepoint's Document Id Service being activated in order to get a link to the document. // class DataReader { private const bool USE_SHAREPOINT_ONLINE = false; private const string SHAREPOINT_URL = "http://companyweb/SoftwareEngineering"; private const string SHAREPOINT_ONLINE_URL = "https://digmap.sharepoint.com"; private const string ONLINE_USER = "jjwilson@digmap.onmicrosoft.com"; private const string ONLINE_PASSWORD = "jjw#ADF$61"; private const string DOCUMENT_LIBRARY_TITLE = "Test EDMS Library"; // The current page of the Sharepoint Document Library private ListItemCollection _docsList = null; // If there are more than 2000 documents in the document library then the documents will be fetched in // pages of 2000. The next two variables contain the current document within the current page and the // position of that page within the whole Sharepoint list. private int _localPosition = -1; // The current position within this page of the document list private ListItemCollectionPosition _globalPosition = null; // the current position within the // // FetchMoreData gets one more page worth of documents from Sharepoint. // private bool FetchMoreData(bool firstTime) { string contextUrl; if (USE_SHAREPOINT_ONLINE) contextUrl = SHAREPOINT_ONLINE_URL; else contextUrl = SHAREPOINT_URL; using (ClientContext SharePointcontext = new ClientContext(contextUrl)) { if (!firstTime && _globalPosition == null) return false; if (USE_SHAREPOINT_ONLINE) { SecureString passWord = new SecureString(); foreach (char c in ONLINE_PASSWORD.ToCharArray()) passWord.AppendChar(c); SharePointcontext.Credentials = new SharePointOnlineCredentials(ONLINE_USER, passWord); } List docs = SharePointcontext.Web.Lists.GetByTitle(DOCUMENT_LIBRARY_TITLE); CamlQuery query = new CamlQuery(); query.ListItemCollectionPosition = _globalPosition; query.ViewXml = "2000"; _docsList = docs.GetItems(query); SharePointcontext.Load(_docsList); SharePointcontext.ExecuteQuery(); _globalPosition = _docsList.ListItemCollectionPosition; return true; } } public DataReader() { FetchMoreData(true); } public bool ReadNext() { _localPosition++; if (_localPosition < _docsList.Count) return true; else { if (FetchMoreData(false)) { _localPosition = 0; return true; } else return false; } } public string GetField(string FieldName) { if (FieldName == "_dlc_DocIdUrl") { FieldUrlValue link = (FieldUrlValue) _docsList[_localPosition][FieldName]; return link.Url; } else return (string) _docsList[_localPosition][FieldName]; } public string this[string FieldName] { get { return GetField(FieldName); } } } }