Solved

Concatenate two columns using SHAREPOINT OBJECT MODEL?

Posted on 2010-09-21
5
1,072 Views
Last Modified: 2012-05-10
How to Concatenate two columns and create new column using SHAREPOINT OBJECT MODEL?
0
Comment
Question by:aaedla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Microsoft has released remote PowerShell capabilities to all commercial Office 365 customers. So you can be controlled via PowerShell and not from the Office 365 admin center Download Windows PowerShell Module for Lync Online http://www.micros…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

630 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