1

I use a caml query to check whether a file exits in a given folder. When I run the application it gives an error saying "Value does not fall within expected range". What am I doing wrong. I have added the code below. Thanks in advance.

string Librery = "Scans";
string folder = @"/"+Librery+"/"+textBox1.Text.Trim();
ClientContext clientContext = new ClientContext("http://test/sites/LegalDocs");
newFile.Url = "Pens.docx";

 if (!FileExists(clientContext, Librery, newFile.Url, folder))
                {
                    Microsoft.SharePoint.Client.File uploadFile = ii.Files.Add(newFile);
                    clientContext.Load(uploadFile);
                    clientContext.ExecuteQuery();
                }

public bool FileExists(ClientContext clientContext, String list, String fileName, String relativeDir)
    {
        List DocumentsList = clientContext.Web.Lists.GetByTitle(list);

        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = @"<View Scope='Recursive'>
                                <Query>
                                    <Where>
                                        <Eq>
                                            <FieldRef Name='FileLeafRef'></FieldRef>
                                            <Value Type='Text'>" + fileName + @"</Value>
                                        </Eq>
                                    </Where>
                                </Query>
                        </View>";
        camlQuery.FolderServerRelativeUrl = relativeDir; ;
        ListItemCollection listItems = DocumentsList.GetItems(camlQuery);
        clientContext.Load(listItems);
        clientContext.ExecuteQuery();

        return listItems.Count > 0;

    }
0

If you look up the corresponding error in the ULS logs, you see something like this:

Exception occured in scope Microsoft.SharePoint.SPList.GetItems. Exception=System.ArgumentException: Value does not fall within the expected range.     at Microsoft.SharePoint.SPWeb.GetWebRelativeUrlFromUrl(String strUrl, Boolean includeQueryString, Boolean canonicalizeUrl)     at Microsoft.SharePoint.SPListItemCollection.Init(SPList list, SPQuery query, String strViewName)     at Microsoft.SharePoint.SPList.GetItems(SPQuery query)     at Microsoft.SharePoint.ServerStub.SPListServerStub.InvokeMethod(Object target, String methodName, XmlNodeList xmlargs, ProxyContext proxyContext, Boolean& isVoid)     at Microsoft.SharePoint.Client.ServerStub.InvokeMethodWithMonitoredScope(Object target, String methodName, XmlNodeList args, ProxyContext proxyContext, Boolean& isVoid)

The FolderServerRelativeUrl means server relative, not site relative. In your case it means:

string folder = @"/sites/LegalDocs/"+Librery+"/"+textBox1.Text.Trim();
  • It worked for me. – Anshu Aug 10 '16 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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