SharePoint 2010 - how to make multi-column a unique value - sharepoint

i'm implementing a sand box solution where is should have more than one column a unique key, i have to use the item adding event receiver but how to get the current adding item field values to know if this item is occurred within the list.
thanks

Create UniqueID column and make it unique.
Create an event receiver as follows:
public override void ItemAdding(SPItemEventProperties properties)
{
string Name = properties.AfterProperties["Name"].ToString();
string Title = properties.AfterProperties["Title"].ToString();
StringBuilder StringBuilder = new StringBuilder(Name);
StringBuilder.Append("-");
StringBuilder.Append(Title);
properties.AfterProperties["UniqueID0"] = StringBuilder.ToString();
base.ItemAdding(properties);
}

Related

Adding multiple objects to ArrayList that match a selected string

What I am trying to do: Fill the namesArray with all the objects that match a certain criteria.
The Activity object is made of four strings "category, name, note, time"
The getCategory(activityArray) returns a string which is the selected category, I'm trying to sort the objects where their category matches the selected category into the new namesArray
So this namesArray will end up containing only Activity objects with selected category.
This is my code:
public static void viewActivity(ArrayList<Activity> activityArray) {
String categ = getCategory(activityArray);
String holder;
ArrayList<Activity> namesArray = new ArrayList<Activity>();
for(Activity obj : activityArray) {
holder = obj.getCategory();
if(holder == categ) {
System.out.println(obj.getName());
namesArray.add(obj);
}
}
}
When I run the debugger, first iteration works, and the first object where it's category matches the selected category is added to the namesArray. But then the if statement just seems to stop working, the holder String does not change and stays the same.
So how do I get my method to add every matching object to the namesArray instead of only the first one?
Thanks
Solved by changing the
if(holder == categ) {
to
if(holder.equals(categ)) {

jxls collection printing order

This is my excel template. Unfortunately, employee.email is ArrayList and can have more than one value.
The result becomes
Actually, aaa1.gmail.com and aaa2.gmail.com belong to AAA and similarly, bbb1.gmail.com and bbb2.gmail.com belong to BBB. The excel output is very misleading in this case.
Is it possible to get as below?
Since I am very new to jxls, any help is really appreciate.
First you need to have a POJO like below to hold the data.
public class Employee {
private int id = 0;
private String name = null;
private List<String> mails = new ArrayList<String>();
// getters & setters
}
Now create data objects that you want to display in your excel file
Employee emp1 = new Employee();
emp1.setId(1);
emp1.setName("AAA");
emp1.getMails().add("aaa1#xyz.com");
emp1.getMails().add("aaa2#xyz.com");
Employee emp2 = new Employee();
emp2.setId(2);
emp2.setName("BBB");
emp2.getMails().add("bbb1#xyz.com");
emp2.getMails().add("bbb2#xyz.com");
List<Employee> employees = new ArrayList<Employee>();
employees.add(emp1);
employees.add(emp2);
then add the data into java.util.Map
Map<String, List<Employee>> beanParams = new HashMap<String, List<Employee>>();
beanParams.put("employees", employees);
then create XLSTransformer object and set the source file, destination file and the map which is holding your data
XLSTransformer former = new XLSTransformer();
former.transformXLS(srcFilePath, beanParams, destFilePath);
you need to use two <jx:forEach> tags to iterate over the list which is in your map and then you can set the values into your result excel file.
Change your excel template file with the following
and the final result will look like

How to get count of a generic list in C# and no need to consider items with null or empty values in the count?

I need to get the count of a generic list in C#.No need to consider empty and null items in the count of list.
Below is my code
Public class Student
{
public string Name {get;set;}
public string Age {get;set;}
}
List<student> listStudent = new List<student>();
Student studentOne=new Student();
studentOne.Name="abc";
studentOne.Age="20";
listStudent.Add(studentOne);
Student studentTwo=new Student();
studentOne.Name="def";
studentOne.Age="22";
listStudent.Add(studentTwo);
Student studentThree=new Student();
studentOne.Name=" ";
studentOne.Age=null;
listStudent.Add(studentThree);
I have written below code to get the count
listStudent.count
It returns 3.it is correct as it contains 3 rows in the list.But I need to get the count of elements or items only having values. here in my code values in last item is null or empty.so I need to get count as 2.
Is there any built in method in c# to do the same. is there any way to check without using loops ?
LINQ can help here:
listStudent.Where(
s => !string.IsNullOrWhiteSpace(s.Name) && s.Age != null
).Count();
There is no method in framework that you are looking for. You need to create your own extended method to make this.

How to add to the object of SelectManyCheckbox selected values?

I have an object of SelectManyCheckbox. For this object I set available values in the next way:
SelectManyCheckbox checkbox = new SelectManyCheckbox();
List<Double> selected = new ArrayList<Double>();//this will be used for setting selected values
List<SelectItem> items = new ArrayList<SelectItem>();//this will be used as available values
for (SomeObject av : listOfObjects)
{
SelectItem item = new SelectItem(av.getNumericValue(),
av.getValue());
items.add(item);
if (av.getDefault())
{
selected.add(av.getNumericValue());
}
}
UISelectItems uiItems = new UISelectItems();
uiItems.setValue(items);
checkbox.getChildren().add(uiItems);
checkbox.setSelectedValues(new Double[selected.size()]);
But this way of setting of selected values does not work. Maybe someone know there is the source of the problem?
Your Double[] is empty there, try converting the list to the array like this:
checkbox.setSelectedValues(selected.toArray(new Double[selected.size()]));

What is the correct way to format SPGridView values being displayed?

Problem
As we know, SharePoint saves data in database in plain text. Some fields even have concatenated strings like <id>;#<value> for user fields. Percents are saved as doubles (1.00000000000000 for 100%) and etc.
Ofcourse, I want to display data as they are displayed in lists.
What should I do?
Should I use derived SPBoundField to format values (Which I actually did and it works fine until you want to filter (probably SPBoundField won't format me values because i use ObjectDataSource not list and with reflector I saw if there are SPListItems in datasource, then it formats correctly. Not my case)
alt text http://img199.imageshack.us/img199/2797/ss20090820110331.png
Or must I loop through all the DataTable and format each row accordingly?
What are Your techniques?
Thank you.
Here is how I solved this issue.
<asp:TemplateField HeaderText="Campaign Members">
<ItemTemplate>
<%# RemoveCharacters(Eval("CampaignMembers").ToString())%>
</ItemTemplate>
</asp:TemplateField>
// Make sure declare using System.Text.RegularExpression;
protected string RemoveCharacters(object String)
{
string s1 = String.ToString();
string newString = Regex.Replace(s1, #"#[\d-];", string.Empty);
newString = Regex.Replace(newString, "#", " ");
return newString.ToString();
}
I normaly use ItemTemplates that inherit from ITemplate. With in the ItemTemplate I use the SPFieldxxxValue classes or some custom formating code. This saves looping through the DataTable and the ItemTemplates can be reused.
The ItemTemplates are attached in Column Binding
E.G
// Normal Data Binding
SPBoundField fld = new SPBoundField();
fld.HeaderText = field.DisplayName;
fld.DataField = field.InternalName;
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);
// ItemTemplate Binding
TemplateField fld = new TemplateField();
fld.HeaderText = field.DisplayName;
fld.ItemTemplate = new CustomItemTemplateClass(field.InternalName);
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);
An example of a ItemTemplate
public class CustomItemTemplateClass : ITemplate
{
private string FieldName
{ get; set; }
public CustomItemTemplateClass(string fieldName, string formatString)
{
FieldName = fieldName;
}
#region ITemplate Members
public void InstantiateIn(Control container)
{
Literal lit = new Literal();
lit.DataBinding += new EventHandler(lit_DataBinding);
container.Controls.Add(lit);
}
#endregion
void lit_DataBinding(object sender, EventArgs e)
{
Literal lit = (Literal)sender;
SPGridViewRow container = (SPGridViewRow)lit.NamingContainer;
string fieldValue = ((DataRowView)container.DataItem)[FieldName].ToString();
//Prosses Filed value here
SPFieldLookupValue lookupValue = new SPFieldLookupValue(fieldValue);
//Display new value
lit.Text = lookupValue.LookupValue;
}
}
Here are a few options. I don't know the output of all of them (would be a good blog post) but one of them should do what you want:
SPListItem.GetFormattedValue()
SPField.GetFieldValue()
SPField.GetFieldValueAsHtml()
SPField.GetFieldValueAsText()
It may also be handy to know that if you ever want to make use of the raw values then have a look at the SPField*XYZ*Value classes. For example the form <id>;#<value> you mention is represented by the class SPFieldUserValue. You can pass the raw text to its constructor and extract the ID, value, and most usefully User very easily.
I would suggest either to format the values before binding them to the spgridview. Linq and an anonymous type is preffered or to call a code behind function on the field that needs the formatting upon binding.
DataField='<%# FormatUserField(Eval("UserFieldName")) %>'
or...maybe a templated field?
After all, i did have not know any other solution to loop through DataTable rows and format them accordingly.
If your SPGridView's data source is list, try out SPBoundField.

Resources