SharePoint Create Folder with Conententtype programer

Source: Internet
Author: User

Take a look at the SharePoint tool that was written yesterday, and the requirement is this:

Under SharePoint list subfolder, create 1000 folder, which specifies a special contenttype,contenttype that has a taxonomy type field, which assigns a value to this filed, And the newly created folder can only create a file of the specified contenttype.

The code to create the folder is as follows:

  Public Static voidCreatfolder (stringSiteURL)//incoming site URL {using(SPSite site =NewSPSite (SiteURL)) {                using(SPWeb Web =site. OpenWeb ()) {SPList list= Web. Lists.trygetlist ("your list title"); if(List! =NULL) {spsecurity.runwithelevatedprivileges ()={web. Allowunsafeupdates=true; SPFolder Bafolder= list. Rootfolder.subfolders.cast<spfolder>(). FirstOrDefault (f= F.name.equals ("your folder name or root folder", StringComparison.OrdinalIgnoreCase)); if(bafolder.exists) {Taxonomyfieldvalue Taxonomyfieldvalue /c8>=gettaxonomyfieldvalue (site, list);  for(inti =100000; I <101001; i++)                                {                                    stringGvname ="GV"+i; SPListItem NewItem=list.                                    Items.Add (Bafolder.serverrelativeurl, Spfilesystemobjecttype.folder, gvname); Newitem[spbuiltinfieldid.contenttypeid]= list. contenttypes["your content type name"].                                    Id;                                    Newitem.update (); newitem["Title"] =Gvname;
Assigns a value to the taxonomy field newitem["Your taxonomy field internal name"] =Taxonomyfieldvalue; Newitem.update (); List. Update ();
Specify Uniquecontenttypeorder for the newly created folder to create only the file or folder of the ContentType type contained in the Uniquecontenttypeorder under the new folder SPFolder Gvfolder=Newitem.folder; Collection<SPContentType> gvfolderorderedcontenttypes =NewCollection<spcontenttype>(); for(intj =0; J < list. Contenttypes.count; J + +) { if(list. CONTENTTYPES[J]. Name = ="your contenttype name") {Gvfolderorderedcontenttypes.add ( List. CONTENTTYPES[J]); }} Gvfolder.uniquecontenttypeorder=gvfolderorderedcontenttypes; Gvfolder.update (); }} web. Allowunsafeupdates=false; }); } } } }
 Private Statictaxonomyfieldvalue Gettaxonomyfieldvalue (SPSite site, SPList list) {taxonomysession session=Newtaxonomysession (site); TermStore TermStore= Session. termstores["Managed Metadata Service"]; Group Group= termstore.groups[the group name"]; TermSet TermSet= group. termsets["Termset name"]; term= termset.terms["term name"];
Get Taxonomyfieldvalue Taxonomyfield Taxonomyfield= list. fields["Your taxonomy field name"] asTaxonomyfield; Taxonomyfieldvalue Taxonomyfieldvalue=NewTaxonomyfieldvalue (Taxonomyfield); Taxonomyfieldvalue.termguid=Term . Id.tostring (); Taxonomyfieldvalue.label=Term . Name; returnTaxonomyfieldvalue; }

Hope to help everyone!

SharePoint Create Folder with Conententtype programer

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.