Solved

Concatenate two columns using SHAREPOINT OBJECT MODEL?

Posted on 2010-09-21
5
1,045 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
Comment Utility
Hi aaedla,
could you just give me a example that explains your question in a better way?
0
 

Author Comment

by:aaedla
Comment Utility
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
Comment Utility
       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
Comment Utility
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
Comment Utility
Thank you for your post.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

11 Experts available now in Live!

Get 1:1 Help Now