Solved

Concatenate two columns using SHAREPOINT OBJECT MODEL?

Posted on 2010-09-21
5
1,048 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This is a fairly complicated script that will install the required prerequisites to install SCCM 2012 R2 on a server.  It was designed under the functional model in order to compartmentalize each step required, reducing the overall complexity.  The …
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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

12 Experts available now in Live!

Get 1:1 Help Now