Skip to main content
Code updated
Source Link
using System;
using

using System.IO; using System.Linq; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Office2013.Excel; using Microsoft.SharePoint.Client; // The following using directive resolves the ambiguity // between the System.IO.File class and Microsoft.SharePoint.Client.File // class. using ClientOM = Microsoft.SharePoint.Client; using System.Net; using System.Data;

class Program { static private void CopyStream(Stream source, Stream destination) { byte[] buffer = new byte[32768]; int bytesRead; do { bytesRead = source.Read(buffer, 0, buffer.Length); destination.Write(buffer, 0, bytesRead); } while (bytesRead != 0); }

static System.IO;
usingvoid System.Linq;
usingMain(string[] DocumentFormat.OpenXml.Packaging;args)
using DocumentFormat.OpenXml.Wordprocessing;{
using Microsoft.SharePoint.Client;
// The following usingNetworkCredential directivecred resolves= thenew ambiguityNetworkCredential("USERNAME", "PASS", "WINAD");
// between the System.IO.File classstring andSPSite Microsoft.SharePoint= "https://www.ClientCOMPANYPORTAL.FileCOM";
    string DirStruc = "/sp/CorporateITPro/BusinessDocuments/Migration/Templates/Master"; class.
using ClientOM = Microsoft.SharePoint.Client;
using System.Net;
using System.Data;

class Program
{
    staticstring privatedFile void= CopyStream(Stream"MigClassification.xslx";
 source, Stream destination)
{ 
    byte[]ClientContext bufferclientContext;
    clientContext = new byte[32768];ClientContext(SPSite);
    intclientContext.Credentials bytesRead;= cred;
    do
    {Web spWebsite;
    spWebsite = clientContext.Web;

    List bytesReadlist = sourceclientContext.Read(buffer, 0, bufferWeb.LengthLists.GetByTitle(dFile);
         destination.Write(buffer, 
 0, bytesRead  CamlQuery camlQuery = new CamlQuery();
    } whilecamlQuery.ViewXml =(bytesRead"<View !=Scope='RecursiveAll'><Query><Where><Neq><FieldRef 0Name='ContentType' /><Value Type='Text'>Folder</Value></Neq></Where></Query></View>");
}    camlQuery.FolderServerRelativeUrl = DirStruc;
    
static void Main(string[] args ListItemCollection listItems = list.GetItems(camlQuery);
{
string SPSite = "https://www clientContext.remotewebisteLoad(listItems, items => items.com";Include(
string dFile = "FileIWantToGet     item => item.xlsx";File.Name,
 // a hard coded variable at the momnent.item => item.File.
 Author,
ClientContext _clientContext;
Web _spWebsite;
NetworkCredential cred = new NetworkCredential("user  item => item.name"File.ServerRelativeUrl, 
 "somePassword"       item => item["Proposalx0020Navigationx0020Grouping"], 
 "RemoteDomain"       item => item["Modified"]);
_clientContext = new ClientContext(SPSite     );
_clientContext.Credentials 
 = cred;
_spWebsite = _clientContext.Web;try
    {
List list = _spWebsite.Lists     clientContext.GetByTitleExecuteQuery(dFile);// NOT FOUND ERROR HERE
CamlQuery camlQuery = new CamlQuery    if (listItems != null);
camlQuery.ViewXml = @"<View Scope='RecursiveAll'><Query><Where>     {
            foreach (Microsoft.SharePoint.Client.ListItem item in listItems)
        <BeginsWith><FieldRef Name='FileDirRef'/>
   {
                item.File.CheckOut();
         <Value Type='Text'>/subdir0/subdir1/subdir2/subdir3/" + dFile + @"</Value>  clientContext.ExecuteQuery();

                // GET SOME DATA RELATED TO THE FILE FROM </BeginsWith></Where></Query></View>";A DB
                //camlQuery.FolderServerRelativeUrl =PUT "/";

ListItemCollectionDATA listItemsIN =THE list.GetItems(camlQuery);FILE

_clientContext.Load(listItems, items => items.Include(
    item => item.File.Name,
    item =>  item.File.ServerRelativeUrlCheckIn("", CheckinType.MajorCheckIn);
    item => item["Proposal_x0020_Navigation_x0020_Grouping"],
    item => item["Modified"]));

_clientContext    clientContext.ExecuteQuery();// 
 Errors as con't find dfile file

if (listItems != null)
{
    foreach}
 (Microsoft.SharePoint.Client.ListItem item in listItems)
    {}
    }
    item.CheckOutcatch ();Exception ex)
    {
    // shove some data inif it(System.Diagnostics.Debugger.IsAttached and== 
 true)
        item.CheckIn();{
  

           Console.WriteLine(itemex.DisplayNameMessage);
    }
    }
    }        
}

}

using System;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.SharePoint.Client;
// The following using directive resolves the ambiguity
// between the System.IO.File class and Microsoft.SharePoint.Client.File
// class.
using ClientOM = Microsoft.SharePoint.Client;
using System.Net;
using System.Data;

class Program
{
    static private void CopyStream(Stream source, Stream destination)
{
    byte[] buffer = new byte[32768];
    int bytesRead;
    do
    {
        bytesRead = source.Read(buffer, 0, buffer.Length);
         destination.Write(buffer, 0, bytesRead);
    } while (bytesRead != 0);
}

static void Main(string[] args)
{
string SPSite = "https://www.remotewebiste.com";
string dFile = "FileIWantToGet.xlsx"; // a hard coded variable at the momnent...
 
ClientContext _clientContext;
Web _spWebsite;
NetworkCredential cred = new NetworkCredential("user.name", "somePassword", "RemoteDomain");
_clientContext = new ClientContext(SPSite);
_clientContext.Credentials = cred;
_spWebsite = _clientContext.Web;

List list = _spWebsite.Lists.GetByTitle(dFile);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='RecursiveAll'><Query><Where>
                        <BeginsWith><FieldRef Name='FileDirRef'/>
                            <Value Type='Text'>/subdir0/subdir1/subdir2/subdir3/" + dFile + @"</Value>
                        </BeginsWith></Where></Query></View>";
//camlQuery.FolderServerRelativeUrl = "/";

ListItemCollection listItems = list.GetItems(camlQuery);

_clientContext.Load(listItems, items => items.Include(
    item => item.File.Name,
    item => item.File.ServerRelativeUrl,
    item => item["Proposal_x0020_Navigation_x0020_Grouping"],
    item => item["Modified"]));

_clientContext.ExecuteQuery();// Errors as con't find dfile file

if (listItems != null)
{
    foreach (Microsoft.SharePoint.Client.ListItem item in listItems)
    {
        item.CheckOut(); 
        // shove some data in it and 
 
        item.CheckIn();  

         Console.WriteLine(item.DisplayName);
    }
}
}
}
using System;

using System.IO; using System.Linq; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Office2013.Excel; using Microsoft.SharePoint.Client; // The following using directive resolves the ambiguity // between the System.IO.File class and Microsoft.SharePoint.Client.File // class. using ClientOM = Microsoft.SharePoint.Client; using System.Net; using System.Data;

class Program { static private void CopyStream(Stream source, Stream destination) { byte[] buffer = new byte[32768]; int bytesRead; do { bytesRead = source.Read(buffer, 0, buffer.Length); destination.Write(buffer, 0, bytesRead); } while (bytesRead != 0); }

static void Main(string[] args)
{
    NetworkCredential cred = new NetworkCredential("USERNAME", "PASS", "WINAD");
    string SPSite = "https://www.COMPANYPORTAL.COM";
    string DirStruc = "/sp/CorporateITPro/BusinessDocuments/Migration/Templates/Master";       
    string dFile = "MigClassification.xslx";
    
    ClientContext clientContext;
    clientContext = new ClientContext(SPSite);
    clientContext.Credentials = cred;
    
    Web spWebsite;
    spWebsite = clientContext.Web;

    List list = clientContext.Web.Lists.GetByTitle(dFile);         
    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml =("<View Scope='RecursiveAll'><Query><Where><Neq><FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value></Neq></Where></Query></View>");
    camlQuery.FolderServerRelativeUrl = DirStruc;
    
    ListItemCollection listItems = list.GetItems(camlQuery);

    clientContext.Load(listItems, items => items.Include(
        item => item.File.Name,
        item => item.File.Author,
        item => item.File.ServerRelativeUrl, 
        item => item["Proposalx0020Navigationx0020Grouping"], 
        item => item["Modified"])
        );
 
    try
    {
        clientContext.ExecuteQuery();// NOT FOUND ERROR HERE
        if (listItems != null)
        {
            foreach (Microsoft.SharePoint.Client.ListItem item in listItems)
            {
                item.File.CheckOut();
                clientContext.ExecuteQuery();

                // GET SOME DATA RELATED TO THE FILE FROM A DB
                // PUT DATA IN THE FILE

                item.File.CheckIn("", CheckinType.MajorCheckIn);
                clientContext.ExecuteQuery(); 
            }
        }
    }
    catch (Exception ex)
    {
        if (System.Diagnostics.Debugger.IsAttached == true)
        {
            Console.WriteLine(ex.Message);
        }
    }        
}

}

edited title
Link
Benny Skogberg
  • 25.5k
  • 12
  • 68
  • 163

Sharepoint 2010 CAMLQUERYCAML QUERY Can't find file

Source Link

Sharepoint 2010 CAMLQUERY Can't find file

I'm wanting to check a document out from an SP site, but my code/CAMLQuery can't find it (it's defo there). It stops at the ExecuteNonQuery line, saying it can't find the FileIWantToGet.xlsx I've not done any SP work before nor heard of CAMLQuery until this bit of work.

I have asked this on StackOverFlow but can't move it to here.

Any help appreciated.

using System;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.SharePoint.Client;
// The following using directive resolves the ambiguity
// between the System.IO.File class and Microsoft.SharePoint.Client.File
// class.
using ClientOM = Microsoft.SharePoint.Client;
using System.Net;
using System.Data;

class Program
{
    static private void CopyStream(Stream source, Stream destination)
{
    byte[] buffer = new byte[32768];
    int bytesRead;
    do
    {
        bytesRead = source.Read(buffer, 0, buffer.Length);
        destination.Write(buffer, 0, bytesRead);
    } while (bytesRead != 0);
}

static void Main(string[] args)
{
string SPSite = "https://www.remotewebiste.com";
string dFile = "FileIWantToGet.xlsx"; // a hard coded variable at the momnent...

ClientContext _clientContext;
Web _spWebsite;
NetworkCredential cred = new NetworkCredential("user.name", "somePassword", "RemoteDomain");
_clientContext = new ClientContext(SPSite);
_clientContext.Credentials = cred;
_spWebsite = _clientContext.Web;

List list = _spWebsite.Lists.GetByTitle(dFile);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='RecursiveAll'><Query><Where>
                        <BeginsWith><FieldRef Name='FileDirRef'/>
                            <Value Type='Text'>/subdir0/subdir1/subdir2/subdir3/" + dFile + @"</Value>
                        </BeginsWith></Where></Query></View>";
//camlQuery.FolderServerRelativeUrl = "/";

ListItemCollection listItems = list.GetItems(camlQuery);

_clientContext.Load(listItems, items => items.Include(
    item => item.File.Name,
    item => item.File.ServerRelativeUrl,
    item => item["Proposal_x0020_Navigation_x0020_Grouping"],
    item => item["Modified"]));

_clientContext.ExecuteQuery();// Errors as con't find dfile file

if (listItems != null)
{
    foreach (Microsoft.SharePoint.Client.ListItem item in listItems)
    {
        item.CheckOut(); 
        // shove some data in it and 

        item.CheckIn();  

        Console.WriteLine(item.DisplayName);
    }
}
}
}