Can you explain a bit more about what you are trying to do? Are you working on a datatype / macro ? I had to get id for file path that was in an httpmodule i wrote to secure pdfs. Before serving the pdf i would do some security checks then if all was good serve the document, however i had to get the id of the item from the requested url this is how i did it:
private int getMediaIdFromUrl(string url, HttpContext context){
Ismail's solution works fine, but is a bit hard on the DB: cmsPropertyData does not have an index for dataNvarchar, so the query must do a scan on the table, which can become slow if you have a lots of properties/versions/hits.
You'd better extract the propertyID from the path (is the number after /media, 7632 in your example) and use that to retrieve contentNodeID (I didn't look, but there should be an API to retrieve property's data from its ID).
If you extract the id from the path, this is actually the id from the cmsPropertyData table. This will make for a more efficient SQL query than selecting the dataNvarchar field.
getting media Id from path
if i have a media (file) witha path like:
http://somehost/media/7632/1267_139.pdf
is theer a way that i with the API can get the media ID of the file ?
Mikael
Mikael,
Can you explain a bit more about what you are trying to do? Are you working on a datatype / macro ? I had to get id for file path that was in an httpmodule i wrote to secure pdfs. Before serving the pdf i would do some security checks then if all was good serve the document, however i had to get the id of the item from the requested url this is how i did it:
private int getMediaIdFromUrl(string url, HttpContext context){
Regex reg = new Regex("/media.*");
int id;
try
{
SqlParameter[] sqParams = {new SqlParameter("@url", reg.Match(url).Value)};
string sql = "select contentNodeId from cmsPropertyData where dataNvarchar = @url";
id= (int)SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,sql,sqParams);
return id;
}
catch(Exception ex){
umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,new umbraco.BusinessLogic.User(0),0,"Error from download security handler ->" + ex.Message.ToString());
}
return 0;
}
Regards
Ismail
My problem is basically the same as yours. I was thinking of coding an XSLT extension to do it. Right now i don't know if i have to do it or not. :-)
I can see that you just look it up in the DB, so i assume that there is no API to do it. But a DB lookup is ok for me too.
Thanks
Mikael
if it works dont forget to mark as solution :-}
Ismail
Ismail's solution works fine, but is a bit hard on the DB: cmsPropertyData does not have an index for dataNvarchar, so the query must do a scan on the table, which can become slow if you have a lots of properties/versions/hits.
You'd better extract the propertyID from the path (is the number after /media, 7632 in your example) and use that to retrieve contentNodeID (I didn't look, but there should be an API to retrieve property's data from its ID).
Marco
Hi,
If you extract the id from the path, this is actually the id from the cmsPropertyData table. This will make for a more efficient SQL query than selecting the dataNvarchar field.
Cheers
Steve
is working on a reply...