2

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?

3 Answers 3

1

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");
                    }
                }
            );
        }
0

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" />
3
  • Microsoft's official list of XSLT Global Parameters can be found here: msdn.microsoft.com/en-us/library/ff806158(v=office.14).aspx
    – bkwdesign
    Commented Jan 3, 2013 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
    Commented Jan 3, 2013 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
    Commented Jan 3, 2013 at 23:08
0

Get it from the Global list of parameters found here. The parameter your looking for is "RootSiteUrl" Here is how to use it. Add it to the list of Parameters in the xsl:stylesheet

add the line:

<xsl:param name="RootSiteUrl" />

Then call it with

$RootSiteUrl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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