How to get Content types
I'm developing a windows application, that talks to SharePoint via its built in web services, and i want to get all content types available on a SharePoint site,
I'm trying to use
Web开发者_高级运维.Webs WebsService = new Web.Webs(); WebsService.Credentials=credentials; WebsService.Url="url of the web service"; XmlNode listOfContentTypes = WebsService.GetContentTypes();
If credentials have administrator privileges i can get the list of all the content types available, But if credentials don't have administrator privileges a 401 exception is thrown (not enought permission).
My question is:
How can i get all content types available on a SharePoint site if i don't have administrator priviliges?I did some poking around in Reflector to see what permissions are actually required. Check out the method below, which is down the call chain from the GetContentTypes()
service method:
public string GetContentTypeTemplates()
{
SPWeb web = SPContext.GetContext(HttpContext.Current).Web;
web.CheckPermissions(SPBasePermissions.EmptyMask | SPBasePermissions.ManageLists);
web.CheckPermissions(SPBasePermissions.EmptyMask | SPBasePermissions.AddAndCustomizePages);
return this.GetGeneralContentTypes(web.AvailableContentTypes);
}
From this, we can gather that SharePoint requires that you have ManageLists
and AddAndCustomizePages
permissions to retrieve all content types.
So, one solution for you could be to ensure that the client account has these permissions, perhaps by creating a custom permission level or, if working within a publishing site, adding the account to the Designers group.
If you wish to use the OOB web services, then you will need to provide the credentials (that have sufficient rights) in your calling application.
Web.Webs WebService = new Web.Webs();
WebService.Credentials = new NetworkCredential("username", "password");
XmlNode list = WebService.GetContentTypes();
How you get those credentials is up to you...
You could write a wrapper web service that calls the Webs webservice (or even use the SharePoint object model) with the required credentials. This wrapper service could be deployed to the SharePoint server.
Your custom application could then call the wrapper service without needing to provide the correct credentials.
精彩评论