Web service to retrieve SharePoint List item - c#

We have one SharePoint Server and another server which is having our web service (SharePoint not installed on the machine). We are trying to access SharePoint server list items using our web service. We are getting count of list items but when we go for CAML query, it returns 0 items.
How can we proceed for getting the list item?
here is our code for your reference.
List Device_List = context.Web.Lists.GetByTitle("MasterList");
context.Load(Device_List);
context.ExecuteQuery();
int position = Device_List.ItemCount;
CamlQuery query = new CamlQuery();
query.ViewXml = #"<View><Query><Where><Eq><FieldRef Name='key' /><Value Type='Text'>ConTransDB</Value></Eq></Where></Query></View>";
ListItemCollection itemCollection = Device_List.GetItems(query);
context.Load(itemCollection);
context.ExecuteQuery();
if (itemCollection.Count > 0 && itemCollection != null)
{
string value = itemCollection[0]["value"].ToString();
}

The ViewXml is not complete, it requires you to define the complete view, not just the query. So it would look like:
query.ViewXml = "<View><Query><Where>...</Where></Query></View>";
You can even add more info than just the query, for example:
<View>
<Query>
<Where>...</Where>
<OrderBy>...</OrderBy>
</Query>
<RowLimit>...</RowLimit>
<ViewFields>...</ViewFields>
</View>

Related

CSOM: Caml query doesn't return uploaded documents items

I have a library with a list of Document Set and, inside them, I uploaded documents.
When I search for a specific Document Set I get the result, when I try to search an uploaded document I got an empty list.
The code for the query is:
List list = _clientContext.Web.Lists.GetByTitle(library);
CamlQuery camlQueryForItem = new CamlQuery();
var queryXml = "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>!#itemid</Value></Eq></Where></Query></View>";
camlQueryForItem.ViewXml = queryXml.Replace("!#itemid", itemId);
ListItemCollection oCollection = list.GetItems(camlQueryForItem);
_clientContext.Load(oCollection);
_clientContext.ExecuteQuery();
If a perform a query for a complete list item:
CamlQuery oQuery = CamlQuery.CreateAllItemsQuery();
ListItemCollection oCollection = list.GetItems(oQuery);
I get all the elements of the library, both Document Set and uploaded documents.
I can't figure out what's wrong and why i get two different behavior.
What I'm sure is that FOLDER items are returned, instead FILE not.

update "created by" in sharepoint using csom c#

I want to update "created by" column in sharepoint. For this, i have to do using CSOM only as i do not have sharepoint server dll. I wrote code in this...
ClientContext cc = new ClientContext ("http://sharepoint...");
List list = cc.web.Lists.GetByTitle("sharepointlistname");
cc.Load(list);
ListItem item = list.GetItemById(13);//In this case, i want to update only id=13. I want to know code for all records also.
cc.Load(item);
cc.ExecuteQuery();
item("Created by") = "Dinesh";
item.Update();
When I run above code, am getting this error...
sharepoint list Invalid data has been used to update the list item. The field you are trying to update may be read only
The Created By field internal name is Author and it's a Person field, please update like this:
ClientContext ctx = new ClientContext("http://sp/sites/dev");
List list = ctx.Web.Lists.GetByTitle("MyList");
Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
//CamlQuery to filter items which created in Today
camlQuery.ViewXml =
#"<View>
<Query>
<Where><Eq><FieldRef Name='Created' /><Value Type='DateTime'><Today /></Value></Eq></Where>
</Query>
</View>";
ListItemCollection items = list.GetItems(camlQuery);
ctx.Load(items);
ctx.ExecuteQuery();
User theUser = ctx.Web.EnsureUser("Contoso\\Jerry");
ctx.Load(theUser);
ctx.ExecuteQuery();
foreach (var item in items)
{
item["Editor"] = theUser;
item["Author"] = theUser;
item.Update();
}
ctx.ExecuteQuery();

CamlQuery - How to use Where clause in C#

I'm very new to CamlQuery and need a bit of help getting this to work.
I installed a tool called CamlDesigner to help me generate the XML needed to filter a collection in Sharepoint, but the XML query that CamlDesigner constructs does not work in my C# codebehind. I have an ID field that I am trying to filter by and I simply want to retrieve an item from Sharepoint where ID = 1 (or 2 or 3 or whatever).
Here is the Caml query generated by the designer:
<Where>
<Eq>
<FieldRef Name='ID' />
<Value Type='Counter'>1</Value>
</Eq>
</Where>
Here is my C# code where I am attempting to incorporate this Caml query. The C# works, but it is returning every item from "My SP Coll" as opposed to only returning the item where ID equals 1.
// Sharepoint web service to retrieve categories items there
ClientContext clientContext = new ClientContext("https://myweb.dev.com/SP");
List oList = clientContext.Web.Lists.GetByTitle("My SP Coll");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<Query><Where><Eq><FieldRef Name='ID'/><Value type='Counter'>" + ID.ToString() + "</Value></Eq></Where></Query>";
Microsoft.SharePoint.Client.ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem oListItem in collListItem) {
string ID = oListItem["ID"].ToString();
}
Thanks for the help!
The query needs to be wrapped in a <View>...</View> element, in addition to the <Query> element.
As per the generated query, the field name is ID not Id.
On a side note, make sure you're disposing of the client context.
And of course, to get an item by ID you can bypass this entire process and just use
var item = list.GetItemById(ID);
The ViewXml property just points to a view within a list and doesn't appear to handle filtering.
If you were to set up an already-filtered view and point the ViewXml to that, then you would still grab all items in the view, but since the view itself is filtered, your result set would match it.

Fill a SharePoint List with a list<splistitem>

I'm working in a webpart. I filter (with caml) a SharePoint list and put my results in a List<SPListItem>.
Now, i need to populate another SharePoint List (I created that list in the same code) and i can't find the way to do that.
List<SPListItem> results = new List<SPListItem>() //results have the result of my query
.
.
.
SPList listFiltered = mySite.Lists[newListName]; //listFiltered is my newlist
SPListItemCollection newListItems = listFiltered.Items; //newListItem are the item from my list
foreach (SPListItem item in results)
{
//I don't know how to send my result to my SharePoint list :(
}
You will need to define your other List, then you can add a new SPListItem to that list with the columns that list contains. I am not sure what results is, if that is a typo or not, but I included that in my answer. You would need to change that if results does not exist.
SPList secondList = web.Lists["MyList"];
foreach(SPListItem item in results)
{
SPListItem Item = secondList.Items.Add();
item["Title"] = companyName
item["DateReceived"] = System.DateTime.Now;
item["Description"] = companyDesc;
item.Update();
}

Custom AnnouncementListItem Visual Web Part in Sharepoint 2010

I am trying to create a visual web Part to display the latest 5 announcement list items.
I need the Announcement List Item Title to show up as a link and any attachments(only pictures) to be displayed right above it. I am planning on refreshing the web part every 15-20 minutes to be able to show the latest announcement.
I dont know how and what the best asp control and page design would be to display these items.
Here is the CAML Query with the rest of the code I have:
using (SPSite oSPsite = new SPSite("http://mySharePointWebApp:Port#/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
// Fetch the List
SPList list = oSPWeb.Lists["Announcements"];
SPQuery spQuery = new SPQuery();
//spQuery.Query = "<Where> <Eq> <FieldRef Name='Title' /> <Value Type='Text'></Value> </Eq> </Where>";
spQuery.Query = "";
spQuery.RowLimit = 5;
// Show item in text box
SPListItemCollection oListCollection = list.GetItems(spQuery);
foreach (ListItem oListItem in oListCollection)
{
// **What should I go with here?**
}
}
}
You can use a repeater control like listbox and customize it using item templates.
<asp:ListBox>
<item template>
<div>
<image control/>
<text control/>
</div>
</item template>
</asp:ListBox>
To get the latest 5 announcements, write CAML SPQuery to get top 5 items by ID in descending order.
Check to see if an attachment exist for the announcement. If it does then get the attachment URL and check if it is an image type by looking at the extension.
If image exist then assign the attachment relative url to the image control in the item template. For announcements with no image you can choose to either hide the image control or assign URL to some common image.
You can also use Linq to SharePoint to get the latest 5 announcements. Code should look like this
var top5Announcements = (From a in siteDataContext.Announcements OrderBy a.id descending select a).Take(5)

Categories