Quantcast
Channel: SharePoint 2010 - General Discussions and Questions forum
Viewing all articles
Browse latest Browse all 17574

naga bhushanam

$
0
0

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace LookUpChange.WebPart1
{
    [ToolboxItemAttribute(false)]
    public class WebPart1 : WebPart
    {
        Button btnLookUp = new Button();
        Label lblErr = new Label();
        protected override void CreateChildControls()
        {
            btnLookUp.Text = "LookUpColumn";
            btnLookUp.ToolTip = "Create Lookup relationship";
            btnLookUp.Click += new EventHandler(btnLookUp_Click);
            this.Controls.Add(btnLookUp);

            lblErr.Text = "";
            this.Controls.Add(lblErr);
        }

        void btnLookUp_Click(object sender, EventArgs e)
        {
            try
            {
                lblErr.Text = "Button Click done successfully!";
                Intercultural_Copy_MainCategorytoLookup();

            }
            catch (Exception ex)
            {
                lblErr.Text = "My Caught error :" + ex.StackTrace;
            }          
        }

        private void Intercultural_Copy_MainCategorytoLookup()
        {
            try
            {
                using (SPWeb web = SPContext.Current.Web)
                {

                    web.AllowUnsafeUpdates = true;

                    //List using Lookup field
                    SPList MainList = web.GetList("http://01hw515746:444/sites/POC/Lists/PRDetails");
                    //List with Lookup (parent) values
                    SPList LookupList = web.GetList("http://01hw515746:444/sites/POC/Lists/TestLookup/");

                    SPListItemCollection listItems = MainList.GetItems("Title", "PRNo");
                    string temp = String.Empty;
                    foreach (SPListItem listItem in listItems)
                    {
                        if (listItem["Title"] != null && !String.IsNullOrEmpty(listItem["Title"].ToString()))
                        {
                            temp = listItem["Title"].ToString();
                            if (listItem["PRNo"] == null || String.IsNullOrEmpty(listItem["PRNo"].ToString()))
                            {
                                try
                                {
                                    listItem["PRNo"] = RetrieveLookFieldIDS_InterculturalValidateCopy_MainCategory(temp, LookupList, "Title");
                                    listItem.Update();
                                }
                                catch (Exception Ex)
                                {
                                    string a = Ex.Message;
                                    //int a1 = 0;
                                }

                            }
                            temp = String.Empty;
                        }
                    }
                    web.AllowUnsafeUpdates = false;
                }
            }
            catch (Exception ex)
            {

                lblErr.Text = "My Caught error :" + ex.StackTrace;
            }
        }

        public static SPFieldLookupValueCollection RetrieveLookFieldIDS_InterculturalValidateCopy_MainCategory(string LUValue, SPList sourceList, string sourceColumnName)
        {
            SPFieldLookupValueCollection lookupIds = new SPFieldLookupValueCollection();
            string[] lookups = LUValue.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string lookupValue in lookups)
            {
                SPQuery query = new Microsoft.SharePoint.SPQuery();
                query.Query = String.Format("<Where><Eq><FieldRef Name='{0}'/><Value Type='Text'>{1}</Value></Eq></Where>", sourceColumnName, lookupValue);

                SPListItemCollection listItems = sourceList.GetItems(query);
                foreach (Microsoft.SharePoint.SPListItem item in listItems)
                {
                    SPFieldLookupValue value = new SPFieldLookupValue(item.ID.ToString());
                    lookupIds.Add(value);
                    break;
                }
            }
            return lookupIds;
        }
    }
}


MLA


Viewing all articles
Browse latest Browse all 17574

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>