in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

akhileshtiwari

  • How to Create LookUp Column (SPFieldLookUp) and Associate it to a Field in List?

    Code below is to create Lookup column at the SPWeb level and associate the lookup column to a Field in a  list.

    --------------------------------------------------------------------------------------------------------------------------------------------------------------

    using (SPSite site = new SPSite("http://<site_url>"))
                {
                    using (SPWeb web = site.AllWebs["/"])
                    {
                        SPList list = web.Lists["LookUpList"];                              //List to which LookUp Column will be associated.
                        string fieldName = "Look_Up_1";                                       //Name of the lookup field.
                        web.Fields.AddLookup(fieldName , list.ID, list.ParentWeb.ID, true);
                        web.Update();                                                                 //This step is very important.
                        SPFieldLookup field = web.Fields[fieldName] as SPFieldLookup;
                        field.ShowInDisplayForm = true;
                        field.LookupField = list.Fields["ID"].Title;                           //Field to which the LookUp column will be associated.

                      //Lookup Column can be assigned values from this field only.
                        field.Update();

                   }

              }
     

    --------------------------------------------------------------------------------------------------------------------------------------------------------------
     

    As usual refer to the inline comments for the explanation of the the code.

     Akhilesh Tiwari

  • Query Multiple Lists using SPSiteDataQuery class

    I will take an example of searching files added in last 7 days in 'document library.

     using (SPSite site = new Site("<site_url">)

    {
    using (SPWeb web = site.AllWebs["/"])      //Best practice to use SPWeb in using.
    {

        DataTable dt;
        SPSiteDataQuery Query = new SPSiteDataQuery();      //Instantiate SPSiteDataQuery

        //Date value in CAML query is accepted in a specific format. Below is the format accepted. 

        string str7DaysBackDateTime = (DateTime.Now.Add(new TimeSpan(-7, 0, 0, 0, 0))).ToString("yyyy-MM-ddThh:mm:ssZ");
        string strQuery = String.Format("<Where><Gt><FieldRef Name=\"Modified\" />"
                    +"<Value Type=\"DateTime\">{0}</Value></Gt></Where>"
                    +"<OrderBy><FieldRef Ascending=\"FALSE\" Name=\"Modified\"/></OrderBy>"
                    , str7DaysBackDateTime);
        Query.Query = strQuery;                     //Assign CAML query(without <Query> tag.
        Query.RowLimit = 15;                           //Max Number of rows you want in the result.
        StringBuilder sb = new StringBuilder();
        sb.Append("<Lists>");
        foreach (SPList list in web.Lists)
        {
            if (list.BaseType == SPBaseType.DocumentLibrary)
            {
                sb.Append("<List ID=\"" + list.ID.ToString() + "\"/>");
            }
       }
       sb.Append("</Lists>");
       Query.Lists = sb.ToString();         //The lists on which you want your query to be run.
                       
       dt = web.GetSiteData(Query);

    }

    I guess the above code is selft explanatory and is having enough comments also to explain the things.

     

    akhilesh tiwari
     


Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts