Get Document Path from SharePoint Library in VSTO

n
n

nWhile working with VSTO AddIn, there was a need tonget the source path of the document while it is being opened from a SharePointndocument library.

n

nAfter little efforts, managednto find the property like

n

n

n

nGlobals.ThisAddIn.Application.ActiveDocument.Path;

n

n

n

nBut this works only when you open an existingndocument from the library and not when you try to create new document.

n

nWas wondering for some time that why is this behaviornshown by Word APIs?? But MS forums helped a lot.

n

nThere are two ways to get the document path /nsource URL when you open the new document from document library.
n

n

n1. Get the template of the document and thennretrieve source / path of the template. Generally all template path returns somethingnlike – http://siteurl/doclibname/forms/templatename

n

nTemplatentemplate = Globals.ThisAddIn.Application.ActiveDocument.get_AttachedTemplate()nas Template;

n

nstringnpath = template.Path;

n

n

n

n2. Environment.CommandLine returns the string usingnwhich you can get the source of the document. Some formatting is needed whichncan be done like this

n

npublic string GetDocLibPath()

n

n{

n

n  string path =nstring.Empty;

n

n  string[]ntempArray = Environment.CommandLine.Split(new char[] { ‘”‘ }, StringSplitOptions.RemoveEmptyEntries);

n

n  ifn(tempArray.Length >= 3)

n

n  {

n

nstring[]ntemp2Array = tempArray[2].Split(new char[] { ‘=’ }, StringSplitOptions.RemoveEmptyEntries);

n

n       ifn(temp2Array.Length == 3)

n

n       {

n

n          path = temp2Array[2];

n

n       }

n

n               

n

n   }

n

n  return path;

n

n }

n

n

n

nReferences: MSDNnForums | MSDNnForums

n

Leave a Comment