“Departments” is the Lookup field for the following operations -
Adding a Lookup Item -
SPListItem listItem = myList.Items[0];
listItem["Departments"] = new SPFieldLookupValue(10, “Operations”); // Adding Operations as text and 10 as ID
listItem.Update();
listItem["Departments"] = new SPFieldLookupValue(10, “Operations”); // Adding Operations as text and 10 as ID
listItem.Update();
Retrieving Lookup field for an item-
SPListItem listItem = myList.Items[0];
SPFieldLookupValue lookupFieldText = new SPFieldLookupValue(listItem["Departments"].ToString());
string lookUpValue = lookupFieldText .LookupValue;
SPFieldLookupValue lookupFieldText = new SPFieldLookupValue(listItem["Departments"].ToString());
string lookUpValue = lookupFieldText .LookupValue;
Updating Lookup field for an item -
SPListItem listItem = myList.Items[0];
SPFieldLookupValue lookupFieldText = new SPFieldLookupValue(listItem ["Departments"].ToString());
lookupFieldText .LookupValue = 11; -> updates the value of the Lookup field for listItem
SPFieldLookupValue lookupFieldText = new SPFieldLookupValue(listItem ["Departments"].ToString());
lookupFieldText .LookupValue = 11; -> updates the value of the Lookup field for listItem
Retrieving all the values in the Lookup Field -
DropDownList res = new DropDownList();
SPWeb web = SPControl.GetContextWeb(Context);
web = web.ParentWeb;
SPLookupField myLookup = (SPLookupField)web.Fields[str];
SPSite site = web.Site;
SPWeb lookupWeb = site.AllWebs[myLookup.LookupWebId];
SPList lookupList = lookupWeb.Lists[myLookup.LookupList];
foreach(SPListItem lookupItem in lookupList.Items)
{
res.Items.Add(lookupItem[myLookup.LookupField];
}
SPWeb web = SPControl.GetContextWeb(Context);
web = web.ParentWeb;
SPLookupField myLookup = (SPLookupField)web.Fields[str];
SPSite site = web.Site;
SPWeb lookupWeb = site.AllWebs[myLookup.LookupWebId];
SPList lookupList = lookupWeb.Lists[myLookup.LookupList];
foreach(SPListItem lookupItem in lookupList.Items)
{
res.Items.Add(lookupItem[myLookup.LookupField];
}
Finally, SPquery for Lookup field by ID in sharepoint -
<Query>
<Where>
<Eq>
<FieldRef Name=’Departments’ LookupId=’TRUE’ />
<Value Type=’Lookup’>10</Value>
</Eq>
</Where>
</Query>
<Where>
<Eq>
<FieldRef Name=’Departments’ LookupId=’TRUE’ />
<Value Type=’Lookup’>10</Value>
</Eq>
</Where>
</Query>
No comments:
Post a Comment