Created
November 9, 2017 18:29
-
-
Save nddipiazza/d11423323e5febf8175a1c19e892ac9a to your computer and use it in GitHub Desktop.
Simple sharepoint c# example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Net; | |
| using System.Security; | |
| using Microsoft.SharePoint.Client; | |
| namespace SpPrefetchIndexBuilder | |
| { | |
| class SpPrefetchIndexBuilder | |
| { | |
| public static string defaultSite = "http://localhost"; | |
| public static ClientContext clientContext; | |
| public static CredentialCache cc = new CredentialCache(); | |
| static void Main(string[] args) | |
| { | |
| if (args.Length >= 1 && (args[0].Equals("--help") || args[0].Equals("-help") || args[0].Equals("/help") || args.Length > 3 || args.Length == 2)) | |
| { | |
| Console.WriteLine("USAGE: SpPrefetchIndexBuilder.exe \n Site [domain] [username]"); | |
| } | |
| clientContext = new ClientContext(args.Length > 0 ? args[0] : defaultSite); | |
| if (args.Length > 1) | |
| { | |
| Console.WriteLine("Please enter password for {0}", args[2]); | |
| NetworkCredential nc = new NetworkCredential(args[2], GetPassword(), args[1]); | |
| cc.Add(new Uri(args[0]), "NTLM", nc); | |
| clientContext.Credentials = cc; | |
| } | |
| Web oWebsite = clientContext.Web; | |
| clientContext.Load(oWebsite); | |
| clientContext.ExecuteQuery(); | |
| ListCollection lists = oWebsite.Lists; | |
| clientContext.Load(lists); | |
| clientContext.ExecuteQuery(); | |
| Console.WriteLine("Web name: '{0}'", oWebsite.Title); | |
| foreach (List list in lists) | |
| { | |
| DownloadDocLib(list.RootFolder); | |
| } | |
| Console.WriteLine("Press any key to continue"); | |
| Console.ReadLine(); | |
| } | |
| static void DownloadDocLib(Folder rootFolder) | |
| { | |
| clientContext.Load(rootFolder.Files); | |
| clientContext.ExecuteQuery(); | |
| foreach (File file in rootFolder.Files) | |
| { | |
| Console.WriteLine("Got a file {0}", file.Name); | |
| } | |
| } | |
| static public SecureString GetPassword() | |
| { | |
| var pwd = new SecureString(); | |
| while (true) | |
| { | |
| ConsoleKeyInfo i = Console.ReadKey(true); | |
| if (i.Key == ConsoleKey.Enter) | |
| { | |
| break; | |
| } | |
| else if (i.Key == ConsoleKey.Backspace) | |
| { | |
| if (pwd.Length > 0) | |
| { | |
| pwd.RemoveAt(pwd.Length - 1); | |
| Console.Write("\b \b"); | |
| } | |
| } | |
| else | |
| { | |
| pwd.AppendChar(i.KeyChar); | |
| Console.Write("*"); | |
| } | |
| } | |
| return pwd; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment