Cant browse through results of GetListItems using LINQ to XML - sharepoint

I'm getting the data from sharepoint wbesite using
activeItemData = ws.GetListItems(listGUID, activeItemViewGUID,
qNode, vNode, rowLimit, null, "");
Here is what I got from the sharepoint its one record.
<**rs:data** ItemCount="1" ListItemCollectionPositionNext="Paged=TRUE&p_ID=1" xmlns:rs="urn:schemas-microsoft-com:rowset">
<**z:row** ows_ID="1" ows_Title="My RFC Title number one" ows_GUID="{A73B8E91-98BF-4CA7-8ADB-A3B933D6D8DA}" ows_Req_x0020_Number="112343" ows_Ends="2010-07-17 00:00:00" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_Start="2010-07-08 00:00:00" ows_owshiddenversion="13" ows_UniqueId="1;#{EA9E9C87-1B28-47DE-B7F4-DA4ADDF913F6}" ows_FSObjType="1;#0" ows_Created="2010-07-07 11:05:56" ows_Status="1. In-Work" ows_FileRef="1;#sites/PS/Lists/Change Control/1_.000" xmlns:z="#RowsetSchema" />
</rs:data>
Now to use LINQ to XML I change the object which hold this data from XMLDocument to XDocument by using
XDocument results = XDocument.Parse(activeItemData.OuterXml);
and the problem is that once I try to browse through the data to obtain ID and Title I got the error: Object reference not set to an instance of an object.
Here is the piece of code which I'm trying to use to get those fields:
var items = from item in results.Descendants(XName.Get("row", "#RowsetSchema"))
select new
{
Title = item.Attribute("Title").Value,
Id = item.Attribute("ID").Value
};
Seems that item is unable to locate this XName element
Thanks

Try:
var items = from item in results.Descendants(XName.Get("row", "#RowsetSchema"))
select new
{
Title = item.Attribute("ows_Title").Value,
Id = item.Attribute("ows_ID").Value
};
If you look at the row element:
<z:row ows_ID="1" ows_Title="My R.....
you were missing ows_ from the attribute name.

Related

Sharepoint C# How to retrieve all pictures and their name by Title of Picture library

Who knows about how to get the image by the Title of the Picture Library (List) using SP C #. Can guide me.
Hope everybody know about SharePoint can help me.
Thanks so much.
You can use CSOM to achive this, demo code is here: https://www.codesharepoint.com/csom/get-all-items-in-sharepoint-using-csom
using Microsoft.SharePoint.Client;
using (ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection"))
{
// clientcontext.Web.Lists.GetById - This option also can be used to get the list using List GUID
// This value is NOT List internal name
List targetList = clientContext.Web.Lists.GetByTitle("List Name");
// Optioanlly you can use overloaded method CreateAllItemsQuery(int rowlimits, params viewfields): where using "rowlimits" you can limit the number of rows returned and viewfields will return only specified fields in the result set
// This method will get all the items from all the folders and sub folders including folders and sub folders too
CamlQuery oQuery = CamlQuery.CreateAllItemsQuery();
ListItemCollection oCollection = targetList.GetItems(oQuery);
clientContext.Load(oCollection);
clientContext.ExecuteQuery();
foreach (ListItem oItem in oCollection)
{
Console.WriteLine(oItem["Title"].ToString());
}
}

How to Replace CSV String in LINQ Query , Wanted to Replace CSV with NewLine in LINQ

I have CSV field which is coming from SQL DB. But when shown in gridview i want to replace csv with New Line. I have tried on working out with below code. but still doesnt work on web apps.
var query = (from r in objEntities.Student
select new
{
FullName = r.FullName.Replace(",", System.Environment.NewLine)
}).ToList();
GridView1.DataSource = query;
GridView1.DataBind();
Looking forward!
You need to replace with <br /> : A line break
and also you may need to set HtmlEncode="false" property of the column if it is BoundField
if the column is auto generated to set HtmlEncode as false check below post
Prevent HTML encoding in auto-generated GridView columns
Html does not recognize new lines as actual line breaks when rendered. Instead try this:
var query = (from r in objEntities.Student
select new
{
FullName = r.FullName.Replace(",", "<br />")
}).ToList();

why i cant bind gridview directly from ListItemCollection Object?

I'm new to SharePoint List and i need to bind gridview using list item collection object like below
SPList splEmployees = SPContext.Current.Web.Lists["Employees"];
spgvEmployees.DataSource = splEmployees.Items;//or splEmployees.GetItems();
spgvEmployees.DataBind();
this is not success when i bind it to gridview contains Title and EmployeeID columns but it success when i remove EmployeeID column.
Try to use a SPDataSource instead.
SPDataSource myDatas = new SPDataSource();
myDatas.List = splEmployees;
spgvEmployees.DataSource=myDatas;
spgvEmployees.DataBind();

How to Get the Title of a SharePoint List?

I am trying to retrieve a list from SharePoint using the web services. I ran into the problem described in this blog post, i.e. the GetList method apparently expects to be passed the list's title instead of the list's name (even though the parameter is called "listName"). I have the list's name, but I cannot figure out how to get the list's title. Where can I find that?
I'm using the SharePoint in Office 365, which I believe is 2010.
A little over head but try this code. Its just a sample code, you might want to mould it to your logic.
string listName = "MyList";
Lists.Lists listSvc = new Lists.Lists();
listSvc.UseDefaultCredentials = true;
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(listSvc.GetListCollection().OuterXml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("A", "http://schemas.microsoft.com/sharepoint/soap/");
XmlNode requiredList = xDoc.SelectSingleNode("//A:List[contains(#DefaultViewUrl,'" + listName + "')]", nsmgr);
string listTitle = requiredList.Attributes["Title"].Value;
XmlNode list = listSvc.GetList(listTitle);
strListName: Can be either a list name, such as "Documents", or a GUID of the list, with or without curly braces, in the following format:
{318B9E8F-1EF4-4D49-9773-1BD2976772B6}
you may find more info here - the above info is an excerpt from this document

Read/write Person metadata from a Word doc stored in SharePoint using VBA or VSTO?

Scenario: Document library in SharePoint with column x of "Person or Group" type. From within a VBA macro (or VSTO add-in) we're trying to access the MetaProperty on the document to set/get the user name. Any attempt to access the value via the ContentTypeProperties collection throws a
Type MisMatch error (13).
The Type property of the MetaProperty object says it's msoMetaPropertyTypeUser. I cannot find any examples of how to work with MetaProperties of this type. Anyone have any experience with this?
Thanks!
You should be able to just do something like this:
using (SPSite site = new SPSite("http://yoursite/subsite"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["DocLibraryName"];
SPListItemCollection items = list.GetItems(list.Views["All Documents"]);
foreach (SPListItem item in items)
{
item["Modified By"] = "Updated Value";
}
}
}
Any metadata for a document should be available by indexing the column name of the SPListItem.
I did it.
The trick here is actually to know that if you put a string corresponding to the user index in MOSS users in the custom property of the Word document, MOSS will recognize it and find the corresponding user to map the field.
so you just need to call http:///_vti_bin/usergroup.asmx
use the function GetUserInfo and retrieve the user index (ID) from it.
MOSSusergroup.UserGroup userGroupService = new MOSSusergroup.UserGroup();
userGroupService.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Xml.XmlNode node = userGroupService.GetUserInfo(userLogin);
string index = node.FirstChild.Attributes["ID"].Value;

Resources