Not sure why, but I do this, and I get no connection.
private void ConnectToSP()
{
string webUrl = "http://win-a70j4oppt1e/";
clContext = new ClientContext(webUrl);
clContext.Load(clContext.Web);
clContext.Load(clContext.Web.Lists);
clContext.ExecuteQueryAsync(OnConnectSucceeded, OnConnectFailed);
busyIndicatorElement.IsBusy = true;
}
I've attempted a few other methods, but it seems like this should work? This is coming from a Silverlight app, so maybe that's my problem? I'm using the appropriate .dll files for that and I just haven't been able to nail this one down.
Thanks!
// callbacks
private void OnConnectSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(FillGrid);
}
private void OnConnectFailed(object sender, ClientRequestFailedEventArgs args)
{
Dispatcher.BeginInvoke(NotifyFailure);
}
public MainPage()
{
InitializeComponent();
// Drop Event Handler
MainGridElement.Drop += new DragEventHandler(SPListsGrid_Drop);
ConnectToSP();
}
// ConnectToSP
private void ConnectToSP()
{
string webUrl = "http://win-a70j4oppt1e/";
clContext = new ClientContext(webUrl);
clContext.Load(clContext.Web);
clContext.Load(clContext.Web.Lists);
clContext.ExecuteQueryAsync(OnConnectSucceeded, OnConnectFailed);
busyIndicatorElement.IsBusy = true;
}
// callbacks
private void OnConnectSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(FillGrid);
}
private void OnConnectFailed(object sender, ClientRequestFailedEventArgs args)
{
Dispatcher.BeginInvoke(NotifyFailure);
}
private void NotifyFailure()
{
busyIndicatorElement.IsBusy = false;
MessageBox.Show("query failed!");
}
private void FillGrid()
{
busyIndicatorElement.IsBusy = false;
var isource = from List ls in clContext.Web.Lists.AsEnumerable()
select new InfoList() { Title = ls.Title, Description = ls.Description };
MainGridElement.ItemsSource = isource;
}
public class InfoList
{
public string Title { get; set; }
public string Description { get; set; }
}
// Drop feature
void SPListsGrid_Drop(object sender, DragEventArgs e)
{
if (e.Data == null)
return;
// we have to identify the item in the Grid where the file is dropped
Point loc = e.GetPosition(Application.Current.RootVisual);
var list = VisualTreeHelper.FindElementsInHostCoordinates(loc, MainGridElement);
DataGridRow row = list.FirstOrDefault(i => i is DataGridRow) as DataGridRow;
if (row != null)
{
IDataObject dataObject = e.Data as IDataObject;
FileInfo[] files = dataObject.GetData(DataFormats.FileDrop) as FileInfo[];
InfoList listDetails = row.DataContext as InfoList;
foreach (FileInfo file in files)
{
UploadFile(file, listDetails.Title);
}
}
}
// Upload file
private void UploadFile(FileInfo fileToUpload, string libraryTitle)
{
var web = clContext.Web;
List destinationList = web.Lists.GetByTitle(libraryTitle);
var fciFileToUpload = new FileCreationInformation();
Stream streamToUpload = fileToUpload.OpenRead();
int length = (int)streamToUpload.Length; // get file length
fciFileToUpload.Content = new byte[length];
int count = 0; // actual number of bytes read
int sum = 0; // total number of bytes read
while ((count = streamToUpload.Read(fciFileToUpload.Content, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
streamToUpload.Close();
fciFileToUpload.Url = fileToUpload.Name;
Microsoft.SharePoint.Client.File clFileToUpload = destinationList.RootFolder.Files.Add(fciFileToUpload);
clContext.Load(clFileToUpload);
clContext.ExecuteQueryAsync(OnLoadingSucceeded, OnLoadingFailed);
busyIndicatorElement.IsBusy = true;
}
private void OnLoadingSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(fileUploaded);
}
private void fileUploaded()
{
busyIndicatorElement.IsBusy = false;
MessageBox.Show("OK! File uploaded");
}
private void fileNotUploaded()
{
busyIndicatorElement.IsBusy = false;
MessageBox.Show("Failed to upload!");
}
private void OnLoadingFailed(object sender, ClientRequestFailedEventArgs args)
{
Dispatcher.BeginInvoke(fileNotUploaded);
}