1

I have made new custom list (throught sharepoint), and I have only add couple custom fields, which type is TEXT. I have rename that TEXT example "Extra". I know how I can read list through code. However, how I can check what information I'm reading? Who added the information? Here is picture of my list:

enter image description here

Here is my code:

List<string> list = new List<string>();

Guid webID = SPContext.Current.Web.ID;

Guid siteID = SPContext.Current.Site.ID;

SPSecurity.RunWithElevatedPrivileges(delegate() // Code runs as the "SharePoint\system" user

{
using (SPSite site = new SPSite(siteID))
{
    using (SPWeb web = site.AllWebs[webID])
    {
        SPList spList = web.Lists["CList"];
        SPListItemCollection collListItems = spList.Items;

        foreach (SPListItem item in collListItems)
        {
            list.Add(item["Text"].ToString());
        }
    }
}
});

Like you can see that my code allows me read field Text, but how I can check if it movie or music, etc? And how I can check who have add that information?

ADDED:

Burrhus, thank you for your example. =)

But any idea, why foreach crashes: "One or more field types are not installed properly. Go to the list settings page to delete these fields."

query.Query = string.Concat(
                              "<Where><Eq><And>",
                                 "<FieldRef Name='Title'/>",
                                 "<Value Type='TEXT'>Movie</Value>",
                                 "<FieldRef Name='Extra'/>",
                                 "<Value Type='TEXT'>Horror</Value>",
                              "</And></Eq></Where>");
SPListItemCollection collListItems = spList.GetItems(query);

I'm trying to get all movies from my list, which extra field is "Horror".

and about querys, is this correct? (if someone, who has more knowledge, can ensure this?)

1 Answer 1

2

Its the same way. It seems as you are using the default Title field to determine if its music or movie.. and that is a text field. The created by field is Author - you can see the default names here http://blogs.msdn.com/b/michael_yeager/archive/2008/11/03/reference-list-for-internal-field-names.aspx

List<string> list = new List<string>();

Guid webID = SPContext.Current.Web.ID;

Guid siteID = SPContext.Current.Site.ID;

SPSecurity.RunWithElevatedPrivileges(delegate() // Code runs as the "SharePoint\system" user

{
using (SPSite site = new SPSite(siteID))
{
    using (SPWeb web = site.AllWebs[webID])
    {
        SPList spList = web.Lists["CList"];
    SPQuery query = new SPQuery();
        query.Query = string.Concat(
                              "<Where><Eq>",
                                 "<FieldRef Name='Title'/>",
                                 "<Value Type='TEXT'>Movie</Value>",
                              "</Eq></Where>");                   
        SPListItemCollection collListItems = spList.GetItems(query); // You need to use getitems to get specific items look here http://msdn.microsoft.com/en-us/library/ms457534.aspx

        foreach (SPListItem item in collListItems)
        {
            list.Add(item["Author"].ToString()); // it's an internal field so you can use it
        }
    }
}
});
3
  • Can you explain little bit more? Example, if I want print only all movies or I want all item which "User Jack" have added?
    – user20968
    Commented Jan 29, 2014 at 7:46
  • Do I really need to create field "Author"? I thought that I can read the value somehow from item properties?
    – user20968
    Commented Jan 29, 2014 at 8:01
  • Author is the same as created by.. I have added a quick example from your code.
    – Burrhus
    Commented Jan 29, 2014 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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