I try to get some files from a folder, inside a folder inside a list. To do this, I try to use a CalmQuery with a RowLimit because I have lot of files in this folder.
But when I execute the code, it appears that my row limit didn't work.
$list = $context.Web.Lists.GetByTitle($ListName)
$context.Load($list)
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View Scope='RecursiveAll'> " +
"<RowLimit>5000</RowLimit>"+
"<Query>" +
"<OrderBy>"+
"<FieldRef Name='FileLeafRef' Ascending='True' />"+
"</OrderBy>"+
"<Where>" +
"<Eq>"+
"<FieldRef Name='FileLeafRef' />"+
"<Value Type='File'>EDMS API/APICAL Invoice</Value>"+
"</Eq>"+
"</Where>"+
"</Query>"+
"</View>"
$listItems = $list.getItems($query)
$context.Load($listItems)
$context.ExecuteQuery()
Most likely it's not an RowLimit issue but rather the Query expression itself. The query:
<Where>
<Eq>
<FieldRef Name='FileLeafRef' />
<Value Type='File'>{value}</Value>
</Eq>
</Where>
returns items those names match {value}, which in turn does not return the files located under a specific folder which url specified in {value}.
If you want to include only files located in specific folder you could consider at least the following two approaches.
Assume the following structure:
News (sub site)
|
Documents (library)
|
Archive (folder)
Then the following examples demonstrate how to include items from Archive folder:
1 Set folder via CamlQuery.FolderServerRelativeUrl property:
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View Scope='RecursiveAll'> " +
"<RowLimit>20</RowLimit>"+
"<Query>" +
"<OrderBy>"+
"<FieldRef Name='FileLeafRef' Ascending='True' />"+
"</OrderBy>"+
"</Query>"+
"</View>"
$query.FolderServerRelativeUrl = "/News/Documents/Archive"
$listItems = $list.getItems($query)
$context.Load($listItems)
$context.ExecuteQuery()
2 Set folder via FileDirRef property
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View Scope='RecursiveAll'> " +
"<RowLimit>20</RowLimit>"+
"<Query>" +
"<OrderBy>"+
"<FieldRef Name='FileLeafRef' Ascending='True' />"+
"</OrderBy>"+
"<Where>" +
"<Eq>"+
"<FieldRef Name='FileDirRef' />"+
"<Value Type='File'>/News/Documents/Archive</Value>"+
"</Eq>"+
"</Where>"+
"</Query>"+
"</View>"
$listItems = $list.getItems($query)
$context.Load($listItems)
$context.ExecuteQuery()
Related
I'm attempting to create a custom list view in a SharePoint 2016 site collection through PowerShell.
$viewTitle = "newview"
#add the following fields to the list
$viewFields = New - Object System.Collections.Specialized.StringCollection
$viewFields.Add("Title") > $null
$viewFields.Add("Description") > $null
$viewFields.Add("EndDate") > $null
$viewFields.Add("Priority") > $null
$viewFields.Add("EventDate") > $null
$viewFields.Add("Status") > $null
#filter query
$viewQuery = "<Where><Or><Neq><FieldRef Name='_Status' /><Value Type='Choice'>Completed</Value></Neq><Geq><FieldRef Name='DateCompleted1' /><Value Type='DateCompleted1'><Today OffsetDays='-14' /></Value></Geq></Or></Where>"
$viewDefaultView = $false
$viewPaged = $true
$viewRowLimit = 30
$viewType = "Html"
$newview = $list.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView, $viewType, $false)
The view seems to create fine. However when I go to actually look at it, I just get a blank page. If I set the $viewType to "Grid" I can see it. When I have it as "Html" It is blank. How do I use "Html" for this view?
Please make sure that your quert is correct.
In your query <FieldRef Name='DateCompleted1' /><Value Type='DateCompleted1'><Today OffsetDays='-14' />.
It should be like this:
<FieldRef Name='DateCompleted1' /><Value Type='DateTime'><Today Offset='-14' />
I have to programmatically set the formula for a calculated field in a list.
This field has to be the sum of two other fields. Can you provide me with the syntax for the formula that I have to insert in the code? I cannot find any example.
Can you provide me also with a reference for formula syntax because I have also to create another calculated field which is the concatenation of two string fields.
Thank you
Calculated Field Formulas reference:
For your task use simply:
=[Column1] + [Column2]
Create Calculated field with server code reference
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Website_Name"])
{
SPList oList = oWebsite.Lists["MyList"];
SPFieldCollection collFields = oList.Fields;
string strNewFieldName = collFields.Add("MyNewColumn",
SPFieldType.Calculated, false);
SPFieldCalculated strNewField =
(SPFieldCalculated)collFields[strNewFieldName];
strNewField.Formula = "=[Column1]<[Column2]";
strNewField.Update();
}
Create Calculated field with CSOM code reference1, reference2. As you see below you need to remeember about escape special characters in xml.
string formula = "<Formula>=FirstName& \" \" &LastName& \" (id: \" &EmployeeID& \" \"</Formula>"
+ "<FieldRefs>"
+ "<FieldRef Name='FirstName' />"
+ "<FieldRef Name='LastName' />"
+ "<FieldRef Name='EmployeeID' />"
+ "</FieldRefs>";
string schemaCalculatedField = "<Field ID='<GUID>' Type='Calculated' Name='FullName' StaticName='FullName'
DisplayName='Full Name' ResultType='Text' Required='TRUE' ReadOnly='TRUE'>" + formula + "</Field>";
Field fullNameField = demoList.Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddFieldInternalNameHint);
clientContext.ExecuteQuery();
Working code for your implementation (comments):
string formula = "<Formula>=Nome1&" "&Cognome1&"(id:"&Campo1&")"</Formula>" + "<FieldRefs>" + "<FieldRef Name='Nome1'/>" + "<FieldRef Name='Cognome1'/>" + "<FieldRef Name='Campo1'/>" + "</FieldRefs>";
string schemaCalculatedField = "<Field ID='1F2ABCC0-D243-40F0-A18D-E0AEF7FE3EB6' Type='Calculated' Name='FullName' StaticName='FullName' DisplayName='Full Name' ResultType='Text' Required='TRUE' ReadOnly='TRUE'>" + formula + "</Field>";
Field fullNameField = context.Web.Lists.GetByTitle("PnP Custom List3").Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddFieldInternalNameHint);
context.ExecuteQuery();
I want to get the Image URL from picture library and from the folders inside the library in SharePoint. I have tried both internal names ows_FileRef and ows_EncodedAbsUrl.
For ows_FileRef it is showing id and # in front of the image url.
For ows_EncodedAbsUrl it is showing as undefined.
Please help me to figure it out.
You can use the following code to iterate through folders as per your requirement:
SPFolder folder = ListPicture.RootFolder.SubFolders["picLib"];
And then you can use SpQuery to get the data:
string SPtestQuery = "<Where><Eq><FieldRef Name='ContentTypeId'/><Value Type='ContentTypeId'>"
+ GlobalConfiguration.Current.Items[Constants.ConfigurationKeys.ContentTypeID]
+ "</Value></Eq></Where><OrderBy><FieldRef Name='Date' Ascending='FALSE' /></OrderBy>";
string SPTestFields = string.Concat(
"<FieldRef Name='FileLeafRef'/>",
"<FieldRef Name='FileDirRef'/>",
"<FieldRef Name='ID'/>",
"<FieldRef Name='GalleryImage'/>",
"<FieldRef Name='Title'/>");
string listUrl = SPUrlUtility.CombineUrl(web.Url, "picLib");
You can get the data in datatable.
Using client object model, with below caml query able to fetch items with in a folder, but seeing a way to get "folder" field values where these items or documents is residing.
+ "<Query>"
+ " <Where>"
+ " <Eq><FieldRef Name='FSObjType' /><Value Type='int'>0</Value></Eq>"
+ " </Where>"
+ "</Query>"
My code for retrieving folder information...
string strFieldValue = string.Empty;
CamlQuery qryFolder = new CamlQuery();
qryFolder.ViewXml = #"<View Scope='RecursiveAll'>"
+ "<Query>"
+ " <Where>"
+ " <And>"
+ " <Eq><FieldRef Name='FSObjType' /><Value Type='int'>1</Value></Eq>"
+ " <Eq><FieldRef Name='FileRef' /><Value Type='Text'>"+folderName+"</Value></Eq>"
+ " </And>"
+ " </Where>"
+ "</Query>"
+ "<ViewFields>"
+ "<FieldRef Name='Title' /><FieldRef Name='FieldValue' /><FieldRef Name='FileRef' />"
+ "</ViewFields>"
+ "</View>";
qryFolder.FolderServerRelativeUrl = rootFolder;//+#"/"+folderName;
ListItemCollection itemColl = docs.GetItems(qryFolder);
context.Load(itemColl);
context.ExecuteQuery();
if (itemColl.Count == 1)
{
strFieldValue = itemColl[0]["FieldValue"].ToString();
}
return strFieldValue
I get a value here when used caml query with FSObjType is 1 which is for only folders... but unfortunately i get null when query with FSObjType is 0 which queries only files. My requirement is to get a value even when you are at the file level... Not sure if i'm going correct with the CAML query..
Thanks,
Jameel
add to you query this "<View Scope=\"RecursiveAll\"> ", just before the + "<Query>"
more details https://sharepoint.stackexchange.com/questions/29405/get-items-under-folder-caml
I would like to update a list item using SharePoint and am stuggling to find 1 decent CAML example.
Here is what I want to do, in SQL my query would look something like this
update [table] set field='value' where fieldID = id;
so this would mean I have 1 item in a list I would like to update 1 field on given the ID of that listitem.
I've tried this, but it doesn't work:
batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='DeliveryStatus'>" + newStatus.ToString() + "</Field>" +
"<Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + id + "</Value></Eq></Where></Method>";
I'll add this answer for the community, although it might not answer all your questions.
batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>" + id + "</Field>" +
"<Field Name='DeliveryStatus'>" + newStatus.ToString() + "</Field></Method>";
It seems the first field you specify is the where clause.
I have no idea how you would do any advanced filtering with this (nots or exclusions or in clauses or ranges). But hope this basic info helps.
You don't need use the where clause to update a list item.
atchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='DeliveryStatus'>" + newStatus.ToString() + "</Field>" +
"<FieldRef Name='ID' /><Value Type='Text'>" + id + "</Value></Method>";
The only think you need do is to provide the ID like above.