必须在非泛型静态类中定义扩展方法 [英] Extension method must be defined in a non-generic static class

查看:70
本文介绍了必须在非泛型静态类中定义扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace MoveDocs
{
    public class TimerJobDocMove : SPJobDefinition
    {
        public TimerJobDocMove()
            : base()
        {

        }

        public TimerJobDocMove(string jobName, SPService service, SPServer server, SPJobLockType lockType)
            : base(jobName, service, server, lockType)
        {
            this.Title = "Trips Document Archive Timer Job";
        }
        public TimerJobDocMove(string jobName, SPWebApplication webapp)
            : base(jobName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Trips Document Archive Timer Job";
        }

        public override void Execute(Guid targetInstanceId)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite srcSite = new SPSite("xxxxxxx"))
                    {
                        using (SPWeb srcWeb = srcSite.OpenWeb())
                        {
                            string _taxPayer = "Archive Library";
                            SPList srcList = srcWeb.Lists["yyyyy"];

                            //Item Collection Object getting all the items form the list
                            SPListItemCollection oSpListCln = srcList.Items;

                            foreach (SPListItem item in oSpListCln)
                            {
                                //string dateInString = "01.10.2009";
                                DateTime startDate = DateTime.Parse(item["Modified"].ToString());
                                DateTime expiryDate = startDate.AddDays(180);
                                if (DateTime.Now > expiryDate)
                                {
                                    SPFolder tgtFolder = srcWeb.Folders[_taxPayer];
                                    SPFile f = item.Web.GetFile(item.Url);
                                    string fileUrl = f.Url;

                                    string result = "";
                                    int last = fileUrl.LastIndexOf('/');
                                    int position1 = fileUrl.IndexOf('/');

                                    result = fileUrl.Substring(position1, last - position1);

                                    string folderpath = result;//"1792" + "/" + "Registration" + "/" + "POS";
                                    var folder = EnsureFolder(srcWeb, _taxPayer, folderpath);

                                    srcWeb.AllowUnsafeUpdates = true;
                                    folder.Files.Add(item.File.Name, item.File.OpenBinary(), true);
                                    folder.Update();
                                    f.Delete();
                                }
                            }

                        }
                    }
                });
            }
            catch(Exception ex)
            { 
            }
        }

        /// <summary>
        /// Ensure SPFolder
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listTitle"></param>
        /// <param name="folderUrl"></param>
        /// <returns></returns>
        public static SPFolder EnsureFolder(this SPWeb web, string listTitle, string folderUrl)
        {
            if (string.IsNullOrEmpty(folderUrl))
                throw new ArgumentNullException("folderUrl");
            SPList list = web.Lists.TryGetList(listTitle);
            return CreateFolderInternal(list, list.RootFolder, folderUrl);
        }

        private static SPFolder CreateFolderInternal(SPList list, SPFolder parentFolder, string folderUrl)
        {
            var folderNames = folderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var folderName = folderNames[0];

            var curFolder =
                parentFolder.SubFolders.Cast<SPFolder>().FirstOrDefault(f => System.String.Compare(f.Name, folderName, System.StringComparison.OrdinalIgnoreCase) == 0);
            if (curFolder == null)
            {
                var folderItem = list.Items.Add(parentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder,
                                                folderName);
                folderItem.SystemUpdate();
                curFolder = folderItem.Folder;
            }


            if (folderNames.Length > 1)
            {
                var subFolderUrl = string.Join("/", folderNames, 1, folderNames.Length - 1);
                return CreateFolderInternal(list, curFolder, subFolderUrl);
            }
            return curFolder;
        }

    }
}


面向"扩展方法必须在非通用静态类中定义"在此点(公共类TimerJobDocMove:SPJobDefinition)在sharepoint 2013 Timerjob代码中。


Am facing "Extension method must be defined in a non-generic static class" at  this point (public class TimerJobDocMove : SPJobDefinition) in sharepoint 2013 Timerjob code.

推荐答案

请删除"静态"和" this "在EnsureFolder方法中,删除" static "在CreateFolderInternal方法中。

Please remove the "static" and "this" in EnsureFolder method, and remove "static" in CreateFolderInternal method.

修改代码如下:

public SPFolder EnsureFolder(SPWeb web, string listTitle, string folderUrl)
{
	if (string.IsNullOrEmpty(folderUrl))
		throw new ArgumentNullException("folderUrl");
	SPList list = web.Lists.TryGetList(listTitle);
	return CreateFolderInternal(list, list.RootFolder, folderUrl);
}

private  SPFolder CreateFolderInternal(SPList list, SPFolder parentFolder, string folderUrl)
{
}

最诚挚的问候,

Dennis


这篇关于必须在非泛型静态类中定义扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆