Adding The Document Id To The Document File Name in SharePoint 2010
Adding The Document Id To The Document File Name in SharePoint 2010
2015
Fljoss:
AddingthedocumentidtothedocumentfilenameinSharePoint2010
(http://www.facebook.com/wombit.systemutveckling)
(http://www.twitter.com/wombitsys)
(https://plus.google.com/115253658878919191099)
(http://www.linkedin.com/company/wombitsystemutvecklingab)
SharePointAddingthebuiltinDocumentIDin
thedocumentfilename
(http://www.wombit.se/2013/10/24/sharepoint
2010documentidinfilename/)
October24,2013 TobiasEriksson(http://www.wombit.se/en/author/tobiaseriksson/)
SharePoint(http://www.wombit.se/tag/sharepoint2010/)
13Comments(http://www.wombit.se/2013/10/24/sharepoint2010documentidinfile
name/#disqus_thread)
Idiscoveredthatthishasbeendonemanytimesafterwedidit.Anyway,Iwillsharemyexperience.
Backin2010IwasinvolvedinaprojectwhichconsistedofcreatingastrictSharePointsitewithmetadata
orienteddocumentstorage.Nofolderswhatsoever,exceptfor10documentlibraries.Thisideawasntthat
hardtoimplementconsideringhoweasyitistocreatestaticmetadatainSharePointandaddingitto
ContentTypes.However,ifyouwanttoputalldocumentsinthesamecontainer,youwillhavetouse
uniquefilenames,otherwisefileswithsamenameswilloverwriteeachotherwithnewversions.
Now,howdoyoucreateauniquefilenamethatibasedonthefilenamewhichtheuserhasentered?Well,
youcanalwaysjustaddacustomIDorthelistitemIDintheendofthefilenameoryoucanusefolderslike
normalpeopleandpraythatnooneuploadsadocumentwithidenticalname.
InthiscasewehadalreadydecidedtouseSharePoint2010sbuiltinfeatureforgeneratingDocumentID.
ByusingthiswewillalsobeabletousesomeotherfeaturesthatisconnectedtotheDocumentID,like
searchandtheabilitytocreatedynamicURLs(whichiwillgetbackto).
OneoftherequirementswasthattheusershouldneverhavetoseetheirdocumentinSharePointwithout
theuniqueIDinthefilename.
Solvingtheproblemusingworkflow
Atfirst,wedidthisviaworkflowwhentheItemwascreatedbutweranintosomeissues.
Theworkflowsometimeskickedin10secondsaftertheitemwascreated.Iveseenthatthiscanbe
modified,butevenafterthatittooslow.
Iftheitemwasupdatedintheworkflow,theSystemAccountwouldhavebeenthemodifier.
Whenusers,aftersettingallmetadataandsavedtheform,theoldfilenamefromtheformwas
overwrittenwiththeonesetbytheworkflow.
Solvingtheproblemusingeventreceivers
http://www.wombit.se/2013/10/24/sharepoint2010documentidinfilename/
1/6
27.5.2015
AddingthedocumentidtothedocumentfilenameinSharePoint2010
WhenuploadingadocumentinSharePointitwilltriggeracoupleofevents.Inourcase,wewereinterestedin
ItemAddingandItemAdded.
ItemAdding
WewantedtouseItemAddingwhenrenamingthefilesothatitwasrenamedwhentheeditformwasdisplayed.
Well,thereisnoiteminItemAddingsowerescrewedthere?!IwouldassumethatusingItemAddingwecould
manipulatethedatabeforetheactualcommit,butno.Andnotafterthebasecalleither.
ItemUpdating,ItemUpdated
Willnotbetriggeredwhenuploading.
ItemAdded
ThisistheeventthatistriggeredrightaftertheItemiscommittedtothedocumentlibrary,andthisiswhat
wehadtoworkwith.
YoucantchangetheSPFile.Namebecauseitsonlyget,youwillhavetochangetheNameField
(FileLeafRef)intheSPListItemproperties.Youwillalsohavetotriggeranupdateforthischangetocommit,
rememberthatthiswilltriggertheUpdateevents.
Ifyouwanttopreventtheupdateeventfromtriggeryoucanalwaysjustsetbase.EventFiringEnabledto
falseandbackagainaftertheupdateisdone.Amoreusefulwaytodothis,istomakeanIDisposeable
Classthatyoucanwrapyourupdatecodewithasshownintheexample.Thisisalsosomethingthatyou
couldusetopreventendlessloopsintheupdateevent.
Basicallyitwouldlooklikethis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
publicoverridevoidItemAdded(SPItemEventPropertiesproperties)
{
SPListItemitem=properties.ListItem;
stringdocumentId;
if(item.TryGetDocumentId(outdocumentId))
{
//Preventtheupdateeventsfromfiering
using(newDisableSPItemEvents())
{
//Getthefilenameandfileextension.
varfileName=item[SPBuiltInFieldId.BaseName];
varfileExt=item[SPBuiltInFieldId.File_x0020_Type];
varnewFileName=string.Format("{0}_{1}.{2}",fileName,documentId,fileExt);
//BaseNameireadonlysowewillsetthefilenamebacktoFileLeafRef
item[SPBuiltInFieldId.FileLeafRef]=newFileName;
//Updatethelistitemwithoutcreatinganewversion
item.SystemUpdate();
}
}
base.ItemAdded(properties);
}
publicclassDisableSPItemEvents:SPItemEventReceiver,IDisposable
{
publicDisableSPItemEvents()
{
this.EventFiringEnabled=false;
}
publicvoidDispose()
{
this.EventFiringEnabled=true;
http://www.wombit.se/2013/10/24/sharepoint2010documentidinfilename/
2/6
27.5.2015
32
33
34
35
36
37
38
39
AddingthedocumentidtothedocumentfilenameinSharePoint2010
}
}
publicstaticboolTryGetDocumentId(thisSPListItemitem,outstringdocumentId)
{
documentId=item.Fields.ContainsField("_dlc_DocId")?(string)item["_dlc_DocId"]:null;
returndocumentId!=null;
}
Andtheresultisthis
(http://wombitse.azurewebsites.net/wp
content/uploads/2013/10/Documents.jpg)
Theendresult
Youhavetomakepeopleawarethatiftheywanttouploadadocumentandadditasanewversiontoan
existing,theyhavetorenamethefilelocallywiththeIDbeforeuploading.
AboutTobiasEriksson
http://www.wombit.se/2013/10/24/sharepoint2010documentidinfilename/
3/6
27.5.2015
AddingthedocumentidtothedocumentfilenameinSharePoint2010
0Comments
Wombit
Recommend
Share
Login
SortbyBest
Startthediscussion
Bethefirsttocomment.
WHAT'STHIS?
ALSOONWOMBIT
ComputerVisionTachographcards
Part1
AngularJSDefiningControllerswith
JavaScriptandTypeScript
1comment2yearsago
1comment2yearsago
RogerAlsingnextpart:
DanielLidstrm Nicelyexplainedaboutthe
http://www.wombit.se/2013/10/2...
nestedscopewithngshow,Iwasnotaware
ofthatissue.Doyoubyanychancehavea
linktowheretheAngularJS
SharePointAddingthebuiltin
DocumentIDinthedocumentfilename
SharePointAddingthedocument
versionhistoryintoMSWord
13comments2yearsago
4comments2yearsago
TobiasErikssonHiHector,thiscanbe
HenriMerkesdalVerycreative!Iwould
donebycreatingaWorkflowinSharePoint
designer.Justcreateaworkflowandaddthe
followingaction"SetNameto
havesaidthatitcouldn'tbedone,buthereit
is:)Thanksforsharing!
Subscribe
Popular
AddDisqustoyoursite
Privacy
Recent
SharePointAdding
(http://www.wombit.se/2013/10/24/sharepoint2010documentidinfilename/) thebuiltin
DocumentIDinthe
documentfilename(http://www.wombit.se/2013/10/24/sharepoint2010documentidinfile
name/)
October24,2013
SharePointOnline
(http://www.wombit.se/2013/12/07/sharepoint2013changetitlemicroblog/) ChangetheTitleofthe
newsfeedinthe
http://www.wombit.se/2013/10/24/sharepoint2010documentidinfilename/
4/6
27.5.2015
AddingthedocumentidtothedocumentfilenameinSharePoint2010
SiteFeedWebPart(http://www.wombit.se/2013/12/07/sharepoint2013changetitlemicroblog/)
December7,2013
SharePointAddingthe
(http://www.wombit.se/2013/10/29/addingdocumentversionhistoryword/) documentversion
historyintoMSWord
(http://www.wombit.se/2013/10/29/addingdocumentversionhistoryword/)
October29,2013
SharePoint
(http://www.wombit.se/2014/06/13/sharepointcrudoperationsusingangularjsrest/) 2013/online
CRUD
operationsusingAngularJSandREST(http://www.wombit.se/2014/06/13/sharepointcrud
operationsusingangularjsrest/)
June13,2014
SharePoint
(http://www.wombit.se/2014/06/12/refreshformdigestvalueusingangularjsandrest/) Refresh
Form
DigestValueusingAngularJSandREST(http://www.wombit.se/2014/06/12/refreshformdigest
valueusingangularjsandrest/)
June12,2014
TAGS
Googleindex(http://www.wombit.se/tag/googleindex/) IIS(http://www.wombit.se/tag/iis/)
Bootstrap(http://www.wombit.se/tag/bootstrap/) ASP.Net(http://www.wombit.se/tag/aspnet/)
ContextMenuAction(http://www.wombit.se/tag/contextmenuaction/)
jQuery(http://www.wombit.se/tag/jquery/) jQueryUI(http://www.wombit.se/tag/jqueryui/)
AngularJS(http://www.wombit.se/tag/angularjs/)
Documentmanagement(http://www.wombit.se/tag/documentmanagement/)
C#(http://www.wombit.se/tag/csharp/) JavaScript(http://www.wombit.se/tag/javascript/)
CATEGORIES
Deployment(2)(http://www.wombit.se/category/deployment/)
IIS(1)(http://www.wombit.se/category/iis/)
Programming(11)(http://www.wombit.se/category/programming/)
SEO(1)(http://www.wombit.se/category/seo/)
SharePoint(10)(http://www.wombit.se/category/sharepoint2010/)
Uncategorized(2)(http://www.wombit.se/category/uncategorized/)
ARCHIVE
http://www.wombit.se/2013/10/24/sharepoint2010documentidinfilename/
5/6
27.5.2015
AddingthedocumentidtothedocumentfilenameinSharePoint2010
June2014(http://www.wombit.se/en/2014/06/)April2014(http://www.wombit.se/en/2014/04/)
December2013(http://www.wombit.se/en/2013/12/)November2013(http://www.wombit.se/en/2013/11/)
October2013(http://www.wombit.se/en/2013/10/)
r eb r o
Post/besksadress
Oskarsparken1
70212rebro
Tel:(Telefon)0196090147
S t oc kh olm
Besksadress
Drottninggatan61
11121Stockholm
Tel:(Telefon)0840025687
(https://maps.google.com/maps?ll=59.298698,16.602972&z=9&t=m&hl=de-DE&gl=US&mapclient=apiv3)
Fehler bei Google Maps melden (https://www.google.com/maps/@59.2986981,16.6029718,9z/data=!10m1!1e1!12b1?source=apiv3&rapsrc=apiv3)
Kartendaten 2015 Google
http://www.wombit.se/2013/10/24/sharepoint2010documentidinfilename/
6/6