2

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);
    }
6
  • 1
    What is the error message you get? Can you also post your OnConnectSucceeded and OnConnectFailed? Also can you tell us the DLL that you have referenced? Thanks. Commented Sep 30, 2011 at 1:01
  • I've referenced both of the .dll files.
    – PixelMuse
    Commented Sep 30, 2011 at 20:14
  • threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException I get a ton of these errors across all the methods I have tried to connect.
    – PixelMuse
    Commented Sep 30, 2011 at 20:28
  • I added my success/fail methods in the original post. Thanks.
    – PixelMuse
    Commented Sep 30, 2011 at 20:30
  • I bet you're trying to access a property of a List that's in the collection you load. Let us see some more code.
    – James Love
    Commented Sep 30, 2011 at 21:22

1 Answer 1

1

Declare class level variable:

ListCollection collList = null;

Can you try this:

clContext.Load(collList,lists=>lists.Include(l=>l.Title,l=>l.Description));

By default, Client OM loads Title property. You need to load the other properties of the List object you are trying to access, such as Description, which the above code includes them.

Since you use a class level variable for the list collection, you can check and load the items in the succeeded method.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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