3

我正在尝试将记录添加到 sharepoint 2010 中的“链接”列表中。我想对添加项目进行验证。我是这样写的。 在此处输入图像描述

我试图停止输入任何包含 google 关键字的链接。下面的代码有什么问题吗。我正在为我的练习而做。我认为图像中的代码没有清楚地出现。我在这里写下来。

if (properties.AfterProperties["vti_url"].ToString().Contains("google"))
           {
               properties.ErrorMessage = "You should not enter the google";
                 properties.Cancel = true;
           }

添加后properties.AfterProperties["URL"].ToString().Contains("google")运行良好。但是我的错误页面看起来不太好。这是屏幕截图。这个问题该怎么办。 在此处输入图像描述

4

2 回答 2

1

我会大胆猜测并说properties.AfterProperties["vti_url"]null,因为没有名为“vti_url”的字段......你试过吗:

properties.AfterProperties["URL"]
于 2012-07-25T07:49:10.367 回答
1

enter code hereHinek 提到的评论很可能是正确的

 /// <summary>
    /// Checks the object to see if it's null, if it isn't it will return the string value of the object.
    /// </summary>
    /// <param name="value">(object) The object you want to check.</param>
    /// <returns>(string) The string value of the object.</returns>
    public static string CheckForNullValue(object value)
    {
        string tmpValue = string.Empty;
        try
        {
            if (value != null)
            {
                if (!string.IsNullOrEmpty(value.ToString()))
                {
                    tmpValue = value.ToString();
                }
            }
        }
        catch (Exception exc)
        {
            Error.Log("Failed to check for null value Value passed in" + value, exc, Error.ErrorType_Error);
            throw exc;
        }
        return tmpValue;
    }

string url = CheckForNullValue(properties.AfterProperties["vti_url"])


if (url.Contains("Google"))   
       {   
           properties.ErrorMessage = "You should not enter the google";   
             properties.Cancel = true;   
       }   

希望这可以帮助

于 2012-07-25T07:54:33.400 回答