Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In other words, is there a SPField.ListsFieldUsedIn() equivalent in SharePoint 2007? Ref: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.listsfieldusedin.aspx

share|improve this question
    
I checked both SPField and SPList class members. Nothing in either have any property or method that will give me a connection between the two. Please help. I need to findout whether a given Site Column is already added to the List. –  Tom Mar 8 '12 at 16:56

1 Answer 1

Here's how it's done in SharePoint 2010:

public ICollection<SPFieldTemplateUsage> ListsFieldUsedIn()
{
    List<SPFieldTemplateUsage> list = new List<SPFieldTemplateUsage>();
    using (SPSqlCommand command = new SPSqlCommand())
    {
        using (SqlDataReader reader = this.GetSqlDataForPushdown(command))
        {
            if (reader != null)
            {
                while (reader.Read())
                {
                    SPFieldTemplateUsage item = new SPFieldTemplateUsage();
                    item.WebID = (Guid) reader[0];
                    item.ListID = (Guid) reader[1];
                    list.Add(item);
                }
                reader.Close();
            }
            return list;
        }
    }
}

private SqlDataReader GetSqlDataForPushdown(SPSqlCommand cmd)
{
    SPSite site = this.Fields.Web.Site;
    SqlSession sqlSession = site.SqlSession;
    cmd.SetStoredProcedure("proc_ListsUsingFieldTemplate");
    cmd.Parameters.AddWithValue("@SiteId", site.ID);
    cmd.Parameters.AddWithValue("@FieldId", this.Id.ToByteArray());
    cmd.Parameters.AddWithValue("@BaseTypes", SPBaseFieldId.BaseTypesFromFieldId(this.Id));
    return sqlSession.ExecuteReader((SqlCommand) cmd);
}

I don't have a SharePoint 2007 VM handy to see if the stored proc exists.

The alternative is to loop through all the lists in the site collection and check the Fields collection. It won't be fast but it'll do the trick.

share|improve this answer
    
Thanks for the code Rob. If you could post the loop, I could mark it as answer. In fact, that is exactly what I ended up doing. I had to loop through all the lists in the site collection and use the List.Fields() method. –  Tom Mar 9 '12 at 3:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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