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

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);
    }
share|improve this question
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. –  Chakkaradeep Chandran Sep 30 '11 at 1:01
    
I've referenced both of the .dll files. –  PixelMuse Sep 30 '11 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 Sep 30 '11 at 20:28
    
I added my success/fail methods in the original post. Thanks. –  PixelMuse Sep 30 '11 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 Sep 30 '11 at 21:22

1 Answer 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.

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.