I am currently facing an obstacle where I have to create a webpart that uses CAML to get the latest image from a document library in Visual Studio. May someone share their code with me on how to do that as I am really new to Sharepoint :(
2 Answers
For SharePoint Online, the following CSOM C# code for your reference.
string siteUrl = "https://tenant.sharepoint.com/sites/lz";
var login = "[email protected]";
var password = "Password";
var libraryName="DL";
ClientContext ctx = new ClientContext(siteUrl);
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials(login, securePassword);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View>
<Query>
<Where>
<Or>
<Or>
<Eq><FieldRef Name='DocIcon' /><Value Type='Computed'>jpg</Value></Eq>
<Eq><FieldRef Name='DocIcon' /><Value Type='Computed'>gif</Value></Eq>
</Or>
<Eq><FieldRef Name='DocIcon' /><Value Type='Computed'>png</Value></Eq>
</Or>
</Where>
<OrderBy><FieldRef Name='Created' Ascending='FALSE'/></OrderBy>
</Query>
<RowLimit>20</RowLimit>
</View>";
List list = ctx.Web.Lists.GetByTitle(libraryName);
ListItemCollection listItems = list.GetItems(camlQuery);
ctx.Load(listItems);
ctx.ExecuteQuery();
foreach (var listItem in listItems)
{
Console.WriteLine("FileName: " + listItem["FileLeafRef"]);
}
There are 2 ways to do it using Visual Studio,
Using Clien object model - This code would run on any non-sharepoint server also, application can be windows/console or asp.net application.
Add reference to below 2 dll
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll
// Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("http://SiteUrl");
// Assume the web has a list named "Announcements". List announcementsList = context.Web.Lists.GetByTitle("DocumentLibrary"); // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" // so that it grabs all list items, regardless of the folder they are in. CamlQuery query = CamlQuery.CreateAllItemsQuery(100); ListItemCollection items = announcementsList.GetItems(query); // Retrieve all items in the ListItemCollection from List.GetItems(Query). context.Load(items); context.ExecuteQuery(); foreach (ListItem listItem in items) { // We have all the list item data. For example, Title. label1.Text = label1.Text + ", " + listItem["FileLeafRef"]; }
Using Server object model. Below code would only run in SharePoint server, it can be farm solution visual web part or sandbox solution. Please note below is sample console application.
you have to add reference to Microsoft.SharePoint.dll
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServerObjectModel {
class Program {
static void Main(string[] args) {
using (var site = new SPSite("http://waqasserver/sites/demo")) {
var web = site.RootWeb;
Console.WriteLine(web.Title);
var lists = from SPList list in web.Lists
where list.Hidden == false
select list;
foreach (SPList list in lists) {
Console.WriteLine("\t" + list.Title);
var items = list.Items.OfType<SPListItem>().Take(5);
var isDocLib = list is SPDocumentLibrary;
foreach (SPListItem item in items) {
var value = isDocLib ? item.Name : item.FileLeafRef;
Console.WriteLine("\t\t" + value);
}
}
Console.ReadLine();
}
}
}
}