SharePoint development-Excel data import into a SharePoint custom list (Data View mode)

Source: Internet
Author: User

This article explains a somewhat new SharePoint instance application, has done the project to the party has had the experience, the data is too many, the customer has the Excel, the request realizes the batch import.

It is roughly as follows


This instance is a data view implementation of the borrow list, and the Data view is similar to Excel, so you can edit it directly on it, even paste it directly from Excel, and then click Save to insert the data into the list.

First, we need to have a list of fields that correspond to Excel, select Create List definition and list instance in my instance (how to create a project without much nonsense)

Create method Stamp Here

After you finish writing the list instance, add an event sink that acts on the list to handle the background operation. Add an event receiver, tick the deleted, added, and updated events to complete the creation.



Write the core code, in our actual application, a temporary table is used to process the data here, click on the page to save and then go to the real data table.

<summary>///The item is being deleted. </summary> public override void Itemdeleting (Spitemeventproperties properties) {BASE.I            Temdeleting (properties); SPListItem Item = properties.            ListItem;            Spmipentities entities = new spmipentities (); String uniqueId = Item.            Uniqueid.tostring (); Zy_yezhqdjch_temp Temp = (from P in entities. Zy_yezhqdjch_temp where P.uniqueid.equals (UniqueId) select P).            FirstOrDefault (); Entities.            DeleteObject (temp); Entities.        SaveChanges ();        }///<summary>//have added items. </summary> public override void ItemAdded (Spitemeventproperties properties) {base.            ItemAdded (properties); SPListItem Item = properties.            ListItem;            Spmipentities entities = new spmipentities ();            Zy_yezhqdjch_temp Temp = new Zy_yezhqdjch_temp (); String A = properties.            Userloginname; Get the sign-in personAccount String Username = properties. Userloginname.substring (properties.            Userloginname.indexof ("spmipmp|") + 8); var list = from P in entities.            Sys_user WHERE (p.usercode.equals (username)) Select new {P.usercode, p.zhi_gid};            DataTable dt = iqueryableextensions.todatatable (list); Gets the login ID temp. Chuang_jzh = Convert.ToInt32 (dt.            rows[0]["Zhi_gid"]); Temp. UniqueId = Item.            Uniqueid.tostring (); if (item["owner list Code"] = = null) {temp.            YE_ZHQDBM = "";} else {temp. YE_ZHQDBM = item["Owner List Code"]. ToString (); } if (item["owner manifest name"] = = null) {temp.            YE_ZHQDMCH = "";} else {temp. YE_ZHQDMCH = item["Owner list name"]. ToString (); } if (item["Manifest item feature"] = = null) {temp.            Qing_dxmtzh = "";} else {temp. Qing_dxmtzh = item["manifest item feature"]. ToString (); } if (item["unit"] = = null) {temp.            Dan_w = "";} else {temp. Dan_w = item["unit"]. ToString (); } if (item["list Amount"] = = null) {temp. Qing_DL = 0; } else {temp. QING_DL = Convert.todecimal (item["list Amount"]); } if (item["visa amount"] = = null) {temp. QIAN_ZHL = 0; } else {temp. QIAN_ZHL = Convert.todecimal (item["visa amount"]); } if (item["acceptance amount"] = = null) {temp. YAN_SHL = 0; } else {temp. YAN_SHL = Convert.todecimal (item["acceptance Quantity"]); } if (item["Contract price"] = = null) {temp. HE_TDJ = 0; } else {temp. HE_TDJ = Convert.todecimal (item["contract Unit Price"]); } if (item["owner to sign the unit price"] = = null) {temp. YE_ZHQRDJ = 0; } else {temp. YE_ZHQRDJ = Convert.todecimal (item["owner to sign the unit price"]); if (item["estimated unit price"] = = null) {temp. YU_JJSDJ = 0; } else {temp. YU_JJSDJ = Convert.todecimal (item["estimated unit price"]); } if (item["estimate scale"] = = null) {temp. YU_JSHJBL = 0; } else {temp. YU_JSHJBL = Convert.todecimal (item["Projected reduction ratio"]); if (item["Final price"] = = null) {temp. JUE_SDJ = 0; } else {temp. JUE_SDJ = Convert.todecimal (item["final Unit Price"]); } entiTies.            Addtozy_yezhqdjch_temp (Temp); Entities.        SaveChanges ();        }///<summary>//updated. </summary> public override void ItemUpdated (Spitemeventproperties properties) {base.            ItemUpdated (properties); SPListItem Item = properties.            ListItem;            Spmipentities entities = new spmipentities (); String uniqueId = Item.            Uniqueid.tostring (); Zy_yezhqdjch_temp Temp = (from P in entities. Zy_yezhqdjch_temp where P.uniqueid.equals (UniqueId) select P).            FirstOrDefault (); Get the login user account string Username = properties. Userloginname.substring (properties.            Userloginname.indexof ("spmipmp|") + 8); var list = from P in entities.            Sys_user WHERE (p.usercode.equals (username)) Select new {P.usercode, p.zhi_gid};            DataTable dt = iqueryableextensions.todatatable (list); Gets the login ID temp. Chuang_jzh = Convert.ToInt32 (dt.            rows[0]["Zhi_gid"]);if (item["owner list Code"] = = null) {temp.            YE_ZHQDBM = "";} else {temp. YE_ZHQDBM = item["Owner List Code"]. ToString (); } if (item["owner manifest name"] = = null) {temp.            YE_ZHQDMCH = "";} else {temp. YE_ZHQDMCH = item["Owner list name"]. ToString (); } if (item["Manifest item feature"] = = null) {temp.            Qing_dxmtzh = "";} else {temp. Qing_dxmtzh = item["manifest item feature"]. ToString (); } if (item["unit"] = = null) {temp.            Dan_w = "";} else {temp. Dan_w = item["unit"]. ToString (); } if (item["list Amount"] = = null) {temp. QING_DL = 0; } else {temp. QING_DL = Convert.todecimal (item["list Amount"]); } if (item["visa amount"] = = null) {temp. QIAN_ZHL = 0; } else {temp. QIAN_ZHL = Convert.todecimal (item["visa amount"]); } if (item["acceptance amount"] = = null) {temp. YAN_SHL = 0; } else {temp. YAN_SHL = Convert.todecimal (item["acceptance Quantity"]); } if (item["Contract price"] = = null) {temp. HE_TDJ = 0; } else {temp. HE_TDJ = Convert.todecimal (item["contract Unit Price"]);        }    if (item["owner to sign the unit price"] = = null) {temp. YE_ZHQRDJ = 0; } else {temp. YE_ZHQRDJ = Convert.todecimal (item["owner to sign the unit price"]); if (item["estimated unit price"] = = null) {temp. YU_JJSDJ = 0; } else {temp. YU_JJSDJ = Convert.todecimal (item["estimated unit price"]); } if (item["estimate scale"] = = null) {temp. YU_JSHJBL = 0; } else {temp. YU_JSHJBL = Convert.todecimal (item["Projected reduction ratio"]); if (item["Final price"] = = null) {temp. JUE_SDJ = 0; } else {temp. JUE_SDJ = Convert.todecimal (item["final Unit Price"]); } entities.        SaveChanges (); }

Next, we want to be more automated, to automatically create a data view of this list and set it as the default view for specialized import operations

To handle the FeatureActivated event, the core code in the instance is as follows

SPWeb _web = properties.            Feature.parent as SPWeb; SPList list = _web.            Lists.trygetlist ("Owner List Base import"); SPList listtwo = _web.            Lists.trygetlist ("Internal inventory base import");            SPView newview = null;            SPView newviewtwo = null;            String strquery = Default (String);            String strquerytwo = Default (String);            StringCollection strcol = new StringCollection ();            StringCollection strcoltwo = new StringCollection (); Newview = list. Views.cast<spview> ().            FirstOrDefault (v = v.title = = "Owner list basis");                if (Newview = = null) {strcol.clear ();                Strcol.add ("Title");                Strcol.add ("Ye_zhqdmch");                Strcol.add ("Qing_dxmtzh");                Strcol.add ("Dan_w");                Strcol.add ("Qing_dl");                Strcol.add ("Qian_zhl");                Strcol.add ("YAN_SHL");                Strcol.add ("He_tdj");                Strcol.add ("Ye_zhqrdj"); STrcol.add ("Yu_jjsdj");                Strcol.add ("YU_JSHJBL");                Strcol.add ("Jue_sdj"); Newview = list. Views.add ("Default", Strcol, strquery, and true/**//* support paging */, true/**//* is the default view */, Microsoft.sharepoin                T.spviewcollection.spviewtype.grid, false);                Newview.title = "Owner list basis";            Newview.update (); } newviewtwo = Listtwo.views.cast<spview> ().            FirstOrDefault (v = v.title = = "Internal inventory basis");                if (Newviewtwo = = null) {strcoltwo.clear ();                Strcoltwo.add ("Title");                Strcoltwo.add ("nei_bqdbm");                Strcoltwo.add ("Nei_bqdmch");                Strcoltwo.add ("Xiang_mtzh");                Strcoltwo.add ("Dan_w");                Strcoltwo.add ("GONG_CHSHL");                Strcoltwo.add ("Ding_e");                Strcoltwo.add ("REN_GF");                Strcoltwo.add ("ji_xf");                Strcoltwo.add ("QU_YBM"); StrColtwo.add ("Qu_ymch");                    Newviewtwo = LISTTWO.VIEWS.ADD ("Default", Strcoltwo, Strquerytwo, and true/**//* support paging */, true/**//* is the default view */,                Microsoft.SharePoint.SPViewCollection.SPViewType.Grid, false);                Newviewtwo.title = "Internal inventory basis";            Newviewtwo.update (); }
The last step, apply it on the application page (here I just describe the case, how the application can be chosen by itself), add an IFRAME element on the page, and introduce the URL address of the list view.



SharePoint development-Excel data import into a SharePoint custom list (Data View mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.