Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Concatenate two columns using SHAREPOINT OBJECT MODEL?

Posted on 2010-09-21
5
Medium Priority
?
1,087 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

972 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