Access Page Owner contact email?

Nov 30, 2012 at 1:41 AM
Edited Nov 30, 2012 at 2:09 AM

Hello,

I'm trying to create a page contact email link for our footer that grabs the current page's contact information--either the page owner from the people picker, or the contact email. This footer will be in the MasterPage.

What is the SPService function that would allow me to access the page owner's contact information?

Thanks,

Will

Coordinator
Dec 3, 2012 at 4:00 AM

Will:

If your pages are publishing pages, then the page contact is probably a column in the Pages list. You can get at that using GetListItems for the page in the library. If it's other types of content, the answers vary, but I'd start there.

M.

Dec 3, 2012 at 5:24 AM
Edited Dec 3, 2012 at 8:14 AM

Yep, using publishing pages, either Welcome pages or Article pages, depending on what the users choose. I believe the Site Owner contact is a column that belongs to any Publishing content type. I'll give that a try.

For anyone else looking at this and wanting a direct link to the GetListItems documentation page, here it is. I'll write back here if I figure out a solution.

Dec 3, 2012 at 6:12 AM

OK, I think I got the function. I'd love to know from the coders here if this could be written a little better, as it seems to me I'm querying the entire xml string for a single field and... I dunno is that a waste?

Anyway, obviously I can use that contactEmail however I want.

Next challenge is to be able to get the contactEmail if only the page contact (using people picker) is filled in—I saw on the documentation page that someone wrote in something about that, but I'm not sure how to mix the CAML. Anyway, trial and error....

$().SPServices({
	operation: "GetListItems",
	async: false,
	listName: "Pages",
	CAMLViewFields: "<ViewFields><FieldRef Name='PublishingContactEmail' /></ViewFields>",
	completefunc: function (xData, Status) {
		$(xData.responseXML).SPFilterNode("z:row").each(function() {
			if ($(this).attr("ows_PublishingContactEmail")) {
				var contactEmail = $(this).attr("ows_PublishingContactEmail");
				console.log(contactEmail);
				return false;
			}
		});
	}
});

Dec 3, 2012 at 6:33 AM
Edited Dec 3, 2012 at 7:36 AM

OK, this is interesting. This returns the Page Contact, using the CAMLQueryOptions to expand the user field (thanks fereko). However, I get a long list of contacts; I'm assuming because this is going through the Pages library and returning each contact. So the next question is: how do I get the ID of the current page, and then match it to the ID of the contact being returned? I'm assuming the answer is hidden inside all that xml....

 

$().SPServices({
	operation: "GetListItems",
	async: false,
	listName: "Pages",
	CAMLViewFields: "<ViewFields><FieldRef Name='PublishingContactEmail' /><FieldRef Name='PublishingContact' /></ViewFields>",
	CAMLQueryOptions: "<QueryOptions><ExpandUserField>True</ExpandUserField></QueryOptions>",
	completefunc: function (xData, Status) {
		$(xData.responseXML).SPFilterNode("z:row").each(function() {
			if ($(this).attr("ows_PublishingContactEmail")) {
				var contactEmail = $(this).attr("ows_PublishingContactEmail");
				console.log("ows_PublishingContactEmail:",contactEmail);
			} else if ($(this).attr("ows_PublishingContact")) {
				var contact = $(this).attr("ows_PublishingContact");
				console.log("contact: ",contact);
			}
		});
	}
});
Dec 3, 2012 at 8:02 AM
Edited Dec 3, 2012 at 8:04 AM

OK, if anyone ever needs to get information about the current page, use the _spPageContextInfo object (thanks to John Liu)

var thisPageID =_spPageContextInfo.pageItemId;
console.log("this Page's ID: ", thisPageID);

So... pretty soon I should have a function that loops through the xml matching the page ID to the current page ID, and then return contact info....

Coordinator
Dec 3, 2012 at 1:51 PM

Will:

The current page's info is also on the current URL, of course. You just need to filter for the current page. It looks like you're heading in the right direction.

M.

Dec 3, 2012 at 10:02 PM

True, true, though that then requires a bit more functional writing to get the url path. Maybe one extra line of code, but probably another half hour of testing.

Anyway, late last night I finished the function. I haven't had a chance to test it to see if it works for anonymous users—that's a new question for anyone who knows: Does the SPServices call for the GetListItems work for an internet/extranet facing sharepoint site with anonymous (public) users?

function get_page_contact_email() {
    var thisPageID = _spPageContextInfo.pageItemId;
    var e;
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "Pages",
        CAMLViewFields: "<ViewFields><FieldRef Name='PublishingContactEmail' /><FieldRef Name='PublishingContact' /></ViewFields>",
        CAMLQueryOptions: "<QueryOptions><ExpandUserField>True</ExpandUserField></QueryOptions>",
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {
                if (thisPageID == $(this).attr("ows_ID")) {
                    if ($(this).attr("ows_PublishingContactEmail")) { // if page email is set
                        e = $(this).attr("ows_PublishingContactEmail");
                    } else if ($(this).attr("ows_PublishingContact")) { //otherwise use contact info
                        var contact = $(this).attr("ows_PublishingContact").split(",#");
                        for (var c = 0; c < contact.length; c++) {
                            if (contact[c].indexOf("@") != -1) {
                                e = contact[c];
                            }
                        }
                    } else { //or nothing is set.
                        e = false;
                    }
                }
            });
        }
    });
    return e;
}

Coordinator
Dec 5, 2012 at 4:03 AM
Edited Dec 5, 2012 at 1:18 PM

GetListItems will generally work for anonymous users, as long as anonymous access is enabled, of course. There are some authentication situations which are just so messy that they mess things up.

M.

Dec 5, 2012 at 4:16 AM

Good to know. It will wind up being a matter of testing. I wrote a separate function to transform a "contact page owner" link at the bottom of the site's masterpage that checks if the above get_page_contact_email() function returns anything. If not, it checks if there's a site collection owner (we created a separate solution to allow site owners to enter a user in as an owner), and defaults to a general webmaster email. I won't be able to test that until we restore a mirror of the public production server to my devbox again, but when I find out I'll be able to post back. Maybe I'll post in the entire set of functions in case anyone else wants to ever place a contact on their site that does something similar to what I'm doing.

Coordinator
Dec 5, 2012 at 1:18 PM

It definitely sounds useful, so please do.

M.