Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have the following question:

I'm modifying the XSLT of the search, so it is represented differently etc etc .. in this occasion I also need to get a relative site collection url.

In managed property i found a property SPSiteUrl, but adding it to properties for the xslt didn't help, as i got an error "inconsistent use of schema" etc etc, so how with other words can I retrieve the site collection relative url?

Because else i cannot know the relative url anyhow ...

OR if i'm in search, and i have the direct url to an item, how can i then by javascript decide which is the part of site collection url?

Any suggestions?

share|improve this question

2 Answers 2

When I customize XSLT for list views and forms (either as XSL referenced in the List's schema, or as an *.ascx that I deploy to CONTROLTEMPLATES) I used the following pre-defined parameter. You just declare it.. and then use it.. it's actual value is supplied magically elsewhere (in main.xsl, I think)

 <xsl:param name="HttpVDir"/>

The above param declaration goes near the top of your stylesheet (between the xsl:stylesheet tags, usually preceding any specific xsl:template tags).

Then, if you put this within the context of an xsl:template, it will output the site collection URL:

 <xsl:value-of select="$HttpVDir" />
share|improve this answer
    
Microsoft's official list of XSLT Global Parameters can be found here: msdn.microsoft.com/en-us/library/ff806158(v=office.14).aspx –  bkwdesign Jan 3 '13 at 22:42
    
So, to further answer your question, I would formulate the XSLT to output a tiny javascript block that declares a single JS variable: var siteCollectionUrl = ... which you can then reference in your other javascript logic. –  bkwdesign Jan 3 '13 at 22:46
    
In my further experimentation just now, for XSL files that are referenced in list schema documents, you can dispense with the param declaration and skip right to using the second snippet above. I think this is because XSL files start with an include reference to main.xsl –  bkwdesign Jan 3 '13 at 23:08

For the OR part..

How to get the SC URL from a full URL in JSOM

Sadly the only way is try and error. Here's my code to get the SC URL (I wanted to get fields of a image listitem by its full URL) related SE post

        function getFileProperties(url, fileUrl) {
            //to get the file specific client context
            //you can create the context based on a url
            //the url sadly must match the pure site's url 
            //exmpl. 
            //http://contoso/library/image.jpg does not work
            //http://contoso/library does not work
            //http://contoso does work
            //to get the pure site's url I simply add a try 
            //error query and repeat recursively until no error
            //is thrown or the url does not contain "/"
            var ctx = new SP.ClientContext(url); //get context
            var siteCollection = ctx.get_site(); //try get site to check if context is valid
            ctx.load(siteCollection);
            ctx.executeQueryAsync(
                function () {
                    // context is valid - proceed 
                    var relUrl;
                    if (_spPageContextInfo.siteServerRelativeUrl !== "/") {
                        relUrl = _spPageContextInfo.siteServerRelativeUrl + fileUrl.replace(_spPageContextInfo.siteAbsoluteUrl, '');  //convert to relative url
                    } else {
                        relUrl = fileUrl.replace(_spPageContextInfo.siteAbsoluteUrl, '');  //convert to relative url - is root sitecollection
                    }

                    var file = ctx.get_web().getFileByServerRelativeUrl(relUrl);   //get file
                    ctx.load(file, 'ListItemAllFields');

                    ctx.executeQueryAsync(
                        function () {
                            var listItem = file.get_listItemAllFields();
                            var comment = listItem.get_fieldValues()._Comments;
                            var copyright = listItem.get_fieldValues().wic_System_Copyright;

                            if (comment) {
                               //use field
                            }
                            if (copyright) {
                               //use field
                            }

                            waitDialog.close();
                            if (!comment || !copyright) {
                                Utility.ShowDialog("Error");
                            }
                        },
                        function (sender, args) {
                            console.log(args.get_message()); //errorhandling
                            waitDialog.close();
                            Utility.ShowDialog("Error");
                        }
                    );
                },
                function (sender, args) {
                    //context is invalid - shrink url and try again                
                    url = url.substr(0, url.lastIndexOf("/"));
                    if (url !== "http:/") {
                        getFileProperties(url, fileUrl); //recursive call
                    } else {
                        //else - the url is not compatible
                        waitDialog.close();
                        Utility.ShowDialog("Error");
                    }
                }
            );
        }

Here's whats essential for you

        function getFileProperties(url, fileUrl) {               
            var ctx = new SP.ClientContext(url); //get context
            var siteCollection = ctx.get_site(); //try get site to check if context is valid
            ctx.load(siteCollection);
            ctx.executeQueryAsync(
                function () {
                    // context is valid - proceed                          
                },
                function (sender, args) {
                    //context is invalid - shrink url and try again                
                    url = url.substr(0, url.lastIndexOf("/"));
                    if (url !== "http:/") {
                        getFileProperties(url, fileUrl); //recursive call
                    } else {
                        //else - the url is not compatible
                        waitDialog.close();
                        Utility.ShowDialog("Error");
                    }
                }
            );
        }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.