Hi and thanks for looking!
Background
I have inherited an old .NET project based on SharePoint 2007 and have designed and external core library which merely accesses SP data, therefore making SP just a backend. Yes, I know it would be better to migrate to SQL, but the client doesn't agree.
The previous developers used a simple read method to read the data in a SP list:
SPList list = CurrentRootWeb.Lists["SomeListName"];
And then they access list properties through a dictionary of sorts (i.e. for each item in list, get item["SomeValue"]).
I am not skilled in SharePoint, so I don't know if this is the most efficient manner to go about accessing it's data.
Problem
How do I read LookUp fields with multiple values in SharePoint?
Every property they request seems to want a string in return. So item[SomeString] is okay, but item[SomeList] makes everything barf! I would have thought that a multi-value lookup list column comes in as a serialized or delimited string holding the selected values (example: "red;blue;green"). What am I missing?
Thanks!
If you are specifically interested in LookUp fields with multiple values not just MultiChoice feilds, then following code should help:
item.Fields["LookFieldName"].Type == SPFieldType.Lookup;
SPFieldLookup LookUpField = item.Fields["LookFieldName"] as SPFieldLookup;
if (LookUpField.AllowMultipleValues)
{
SPFieldLookupValueCollection valueCollection = item[Field.Id] as SPFieldLookupValueCollection;
string[] arrLookupValues = (from SPFieldLookupValue val in valueCollection select val.LookupValue).ToArray<string>();
}
For each of the SPFields in the list's fields, you need to test the field's Type.
If the type is SPFieldType.MultiChoice, then you cast the SPField to SPFieldChoice and access the Choices collection, which is a StringCollection.
Related
In my project (I am using azure storage) I have some data that I want to translate. I have the resource system in place for translations. I have a table in cloud which has name property. I want to translate it somehow.
One option is to create all the entries in database for each language which I don't prefer as it would create a lot entries along with the name.
Is there a smart way to use the resx mechanism I have in place?
So the table has multiple properties and one is name. Name could be anything like Mud, rock etc. Now I want to translate Mud into different language. Something like Texts.Mud would return me the correct value.
But lets say I get data like this
var data = some query;
string translatedName = Texts.data[0].name; // this won't work
You should instead add more columns in the database, each for a different language and select the column based on the user language.
Other solution is to have a transaltion mechanism (a custom class for example), where you pass the original database result (say data[0].name) to a query and it returns the translated value for you.
I am having a scenario where I am required to copy the SharePoint list items from one list to another it includes various types of fields such as text, lookup and people group.
I am trying to copy the look-up field from one list to another using the following piece of code but what it does is it copies only the last value in the loop into the other list column but I want all the entire set items copied from one list to another.
foreach (SPFieldLookupValue value in values)
{
targetItem["Hiring_x0020_Manager"] = new SPFieldLookupValue(value.ToString());
}
Is there anyway to copy the entire set of items from one list to another separately.
Also is it possible to copy SP Column type of people and look-up from one list to another using a similar code snippet.
…but what it does is it copies only the last value in the loop…
You keep overwriting the value of the destination field. Hence yes, it behaves like that.
For lookup fields that have multiple values enabled, you have to construct a new SPFieldLookupValueCollection instance and assign it to the destination field. I assume your values variable is exactly of this type. There is an extensive example in MSDN.
I am using Rad grid to display sharepoint library items. When i am displaying ChoiceWith CheckBox type columns, then the values are coming with additional symbols of ;# before and after each choice value.
How can i remove the ;# symbols from ChoiceWithCheckBox type columns ?
Thanks in Advance.
That's how SharePoint stores the values internally. It's basically a key/value pair.
You can manually parse the items to only return the value without the key.
A Lookup field/column in a SharePoint list is stored as a key#value pair internally.
Use SPFieldLookupValue to get either the value(Text) or the key(number) from the lookup field.
SPFieldLookupValue('column name').LookupValue gives you the VALUE in the key#value pair
SPFieldLookupValue('column name').LookupId gives you the KEY in the key#value pair
I guess you are directly binding the SPItemCollections object (List.items or CAML query results) to your telerik radGrid. I usually create a generic list(in memory object), populate the list with items in the format user wants to see and then bind the generic list to radGrid.
If you are working on a SharePoint 2010 list, see if SPRadGrid telerik webpart suits your need
My application has dozens of ComboBoxes filled with lists of values.
I wonder what is the best way to store these values in the DB since the user can add/remove :
Store in XML in Sql Server ?
Store in a text file on the server ?
any best practice ?
Thanks
John
I would keep track of them in a table on SQL server. If a user can define each field, then you simply need a table with a field name, value, and userid. Then you can extrapolate all of it from there.
Of course, you would want to use a key of some kind linked to a table that defines each type of field possible, if needed.
One row per key/value (combobox) should do the trick.
Another option to consider is to serialize your form in a json string that you'll store in a table in the SQL server DB. That way, you'll only have one entry per user to read. Then, you can deserialize the json string in an object that retains the properties of each control.
The big advantage of doing it this way is that if your form changes (and changes do happen!) you won't have to modify the data layer (ie. table definition + query).
I have a table full of id's,categories and weights that I need to reference in my program as I read in records that contain those categories. What is the most efficient method to read those from a database and put into a structure that I can reference?
The ID's (and possibly the names) would be unique
Data might look like:
ID,Category,Weight
1,Assignment,5
2,Test,10
3,Quiz,5
4,Review,3
Your best bet is to read in your table using a DataReader, and put each row into an object containing Category and Weight, then each object into a Dictionary.
If you're using a later version of .NET, you could always use Linq to just grab that data for you.
If you want to avoid a database hit to fetch static data, you can hard-code the values into a common class in your solution. A Dictionary collection would work fine here too.
The trade off of course is; 2 locations to manage for any possible future changes.