Solved

Concatenate two columns using SHAREPOINT OBJECT MODEL?

Posted on 2010-09-21
5
1,052 Views
Last Modified: 2012-05-10
How to Concatenate two columns and create new column using SHAREPOINT OBJECT MODEL?
0
Comment
Question by:aaedla
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:brainteaser
ID: 33732446
Hi aaedla,
could you just give me a example that explains your question in a better way?
0
 

Author Comment

by:aaedla
ID: 33734198
I have two columns...form comments and delete comments.these columns are coming from workflows and multiple line text columns.I need to concatenate these two columns and create one column comments.we can not concatenate using OOB since these columns are multiple line text columns and are not available in Insert Column box.
0
 
LVL 2

Expert Comment

by:Serge_Uly
ID: 33744861
       private static void ConcatFields()
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Build a query.
                    SPQuery query = new SPQuery();
                    query.Query = string.Concat(
                                   "<Where><Eq>",
                                      "<FieldRef Name='ID'/>",
                                      "<Value Type='Number'>1</Value>",
                                   "</Eq></Where>");

                    query.ViewFields = string.Concat(
                                        "<FieldRef Name='_Comments' />",
                                        "<FieldRef Name='DelComments' />");

                    query.ViewFieldsOnly = true; // Fetch only the data that we need.

                    // Get data from a list.
                    const string listUrl = "/lists/CustomList";
                    SPList list = web.GetList(listUrl);
                    SPListItemCollection items = list.GetItems(query);

                    System.Console.WriteLine("Concatenated comment: {0}", string.Concat(items[0]["_Comments"], items[0]["DelComments"]));
                }
            }
            System.Console.ReadLine();
        }
0
 
LVL 2

Accepted Solution

by:
Serge_Uly earned 500 total points
ID: 33745696
Ah, you want to create new column also. This is the code:
        private static void ConcatFields()
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Build a query.
                    SPQuery query = new SPQuery();
                    query.Query = string.Concat(
                                   "<Where><Eq>",
                                      "<FieldRef Name='ID'/>",
                                      "<Value Type='Number'>1</Value>",
                                   "</Eq></Where>");

                    query.ViewFields = string.Concat(
                                        "<FieldRef Name='_Comments' />",
                                        "<FieldRef Name='DelComments' />");

                    // Get data from a list.
                    const string listUrl = "/lists/CustomList";
                    SPList list = web.GetList(listUrl);
                    SPListItemCollection items = list.GetItems(query);
                    SPListItem item = items[0];

                    list.Fields.Add("ConComments", SPFieldType.Note, false);
                    list.Update();

                    item["ConComments"] = string.Concat(item["_Comments"], item["DelComments"]);
                    item.Update();
                 }
            }
            System.Console.ReadLine();
        }
0
 

Author Comment

by:aaedla
ID: 33785452
Thank you for your post.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now