简体   繁体   English

以编程方式查询SharePoint列表

[英]Querying SharePoint Lists Programmatically

What is the best way to interact with SharePoint with code? 用代码与SharePoint交互的最佳方法是什么? Trying to figure out the right area pursuing without wasting time. 尝试找出正确的区域而不浪费时间。 C#? C#? JavaScript? JavaScript的?

Right now I'm just trying to make a web part that is SharePoint List Driven...it will basically create a navigation bar and the names/links across that navigation bar would be displayed on however I have my SharePoint List configured. 现在,我只是想制作一个由SharePoint List Driven驱动的Web部件...它将基本上创建一个导航栏,并且该导航栏上的名称/链接将显示在上,但是我已配置了SharePoint列表。

You can use C# for your webpart feature. 您可以将C#用于Webpart功能。 Make an empty project, then add a webpart (non visual webpart) to the project. 制作一个空项目,然后向该项目中添加一个Web部件(非可视Web部件)。 Use the C# object model to query the SPList object, then do a foreach loop to spit out the values into your nav bar. 使用C#对象模型查询SPList对象,然后执行foreach循环以将值吐出到导航栏中。

The pain with C# comes when you have to update your feature with a different version number for the solution. 当您必须为解决方案使用其他版本号更新功能时,C#会带来痛苦。 That is unless you are excellent with the feature upgrade process. 除非您对功能升级过程非常满意。 The pervious version webpart instance is stuck in WP zones of whatever ASPX they are stored in. I keep my solutions at version 1.0, then note a version number of the build in the feature description, which shows up in the features list in Site Settings. 以前的版本的Webpart实例卡在存储它们的任何ASPX的WP区域中。我将解决方案保持在1.0版,然后在功能说明中记下该版本的版本号,该版本号显示在“站点设置”中的功能列表中。

You can use JavaScript for your web part feature. 您可以将JavaScript用于Web部件功能。 Make an empty project, then add a webpart (non visual webpart) to the project. 制作一个空项目,然后向该项目中添加一个Web部件(非可视Web部件)。 Call the web service for getitems on the specific nav list. 调用Web服务以获取特定导航列表上的getitems。 Visual studio will create a strongly named class for the SP site that the list lives in, that stores the definition of all the lists in that site. Visual Studio将为列表所在的SP站点创建一个名称重命名的类,该类存储该站点中所有列表的定义。 If your nav list changes, refresh the web serivce and update this strongly named class. 如果您的导航列表发生更改,请刷新网络服务并更新此强命名的类。

When your web part runs, the web service will run as the user that is logged in. So either make sure all visitors have view rights, or make a AD service account to call the web service. 当您的Web部件运行时,该Web服务将以登录的用户身份运行。因此,请确保所有访问者都具有查看权限,或者创建一个AD服务帐户来调用该Web服务。 Once your Javascript returns the XML, use a parcer like Lync to XML to get the data and spit out the HTML into the nav bar. Java脚本返回XML后,使用Lync到XML之类的解析器获取数据并将HTML吐出到导航栏中。

It's more about comfortable preference than which is better. 与其说是舒适,不如说是更好。 If you're using SharePoint 2013, make a SP app instead of a farm feature. 如果您使用的是SharePoint 2013,请制作一个SP应用程序而不是服务器场功能。 Then you can only use JavaScript and web services. 然后,您只能使用JavaScript和Web服务。

You can the lists.asmx webservice and javascript, bellow is the code taht I use to query SharePoint lists. 您可以使用list.asmx Web服务和javascript,下面是我用来查询SharePoint列表的代码。

var url = document.URL;
if(url.indexOf("https://") != -1)
{
    var urllink = document.location.href.replace("https://","");
    var prefix = "https://";
}
else
{
    var urllink = document.location.href.replace("http://","");
    var prefix = "http://";
}   
var link = (urllink.split("/"))[0];

$(document).ready(function() {
    var soapEnv =
        "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
                 <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                    <listName>Web Pages</listName> \
                    <viewFields> \
                        <ViewFields> \
                           <FieldRef Name='Title' /> \
                       </ViewFields> \
                    </viewFields> \
                </GetListItems> \
            </soapenv:Body> \
        </soapenv:Envelope>";

    $.ajax({
        url: prefix+link+"/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
});

var names = new Array();
var href = new Array();
var iterator;

function processResult(xData, status) {
    $(xData.responseXML).find("z\\:row, row").each(function() {

    names.push($(this).attr('ows_Title'));
    href.push($(this).attr('ows_FileRef'));

    });
}

I don't have access to the backend of Sharepoint (IT restriction) so I'm only autorized to use JavaScript for this kind of tasks, and it works great with the Sharepoint Web Services. 我无权访问Sharepoint的后端(IT限制),所以我只被允许使用JavaScript来完成此类任务,它可以与Sharepoint Web服务一起很好地工作。

I've created a JavaScript API that is, I think, very useful and easy to use: SharepointPlus 我创建了一个非常有用且易于使用的JavaScript API: SharepointPlus

Otherwise there are some alternatives like the popular SPServices . 否则,有一些替代方法,例如流行的SPServices

So I'd say it depends of your needs, restrictions, skills and of the complexity of what you're trying to do at the end. 因此,我想说这取决于您的需求,限制,技能以及最终要执行的操作的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM