When I upload a file to Sharepoint, I save a record in my App's database with the file name and location, usually this will be in the format http://198.mySharepointServer/LibraryName/Filename.txt. This is then much easier and quicker than accessing Sharepoint programatically, running code and then retrieving the files url because you can just access a local database.
Similarly if you have the file's filename and you are only using 1 Sharepoint library the url shouldn't change so you can just use that url throughout and simply attach /filename.txt to the end of that url.
Here's a short extract of some code I used to retrieve the url programatically by searching with a document ID. Please note this was coded by a colleague so I just followed the logic, you can do the same and see what pieces you could use.
using (ClientContext spclient = new ClientContext(libraryUrl))
{
CamlQuery query = new CamlQuery();
try
{
libraryName = Properties.Settings.Default.Members;
spclient.Credentials = creds;
Web site = spclient.Web;
List docList = site.Lists.GetByTitle(libraryName);
spclient.Load(docList.Fields);
spclient.ExecuteQuery();
var filterList = new FilterOptionList();
var DocID = new FilterOptions();
AddFilters(spclient, docList, filterList, DocID, new string[] { DocumentID }, "Doc Ref No");
StringBuilder q1 = new StringBuilder();
q1.Append(CreateAndSection(filterList));
queryString.Append(q1.ToString());
queryString.Append("</Where></Query></View>");
query.ViewXml = queryString.ToString();
ListItemCollection collListItem = docList.GetItems(query);
spclient.Load(collListItem);
spclient.ExecuteQuery();
if (collListItem.Count != 0)
{
spclient.Load(docList.Fields.GetByTitle("Doc Ref No"));
spclient.ExecuteQuery();
var DocRefNumColName = docList.Fields.GetByTitle("Doc Ref No").InternalName;
foreach (ListItem item in collListItem)
{
if (item.FileSystemObjectType == FileSystemObjectType.File)
{
spclient.Load(item);
spclient.Load(item.File);
spclient.ExecuteQuery();
domain = item.File.ServerRelativeUrl;
if (domain != "")
{
DocumentURL = libraryUrl + domain;
IsInMembers = true;
}
else IsInMembers = false;
}
}
}
}
catch (Exception ex)
{
DocumentURL = "";
}
}
Please vote as an answer if this helped you.